home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / ANSIshellƒ / os_mac_eEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-04  |  87.0 KB  |  3,047 lines  |  [TEXT/R*ch]

  1. /* os_mac_eEdit.c 
  2.  * 8Apr92  e
  3.  */
  4.  
  5. #ifdef THINK_C
  6. #include <MacHeaders>
  7. #else
  8. #ifdef __MWERKS__
  9. #else
  10. "Unknown compiler!"
  11. #endif
  12. #endif
  13.  
  14. #include "os_mac_eEdit.h"
  15.  
  16. /* an editor for text files by e
  17.    questions/comments via Internet <e@Flavors.COM> */
  18. /* Copyright © e, 1992. All rights reserved.
  19.     Developed using THINK C 5.0.1 for use with Gambit Scheme.
  20.     This code may be freely distributed as long as this notice remains.
  21.    based upon CEditor.C...
  22.     Copyright © BRH Toolsmith, 1992. All rights reserved.
  23.     Developed using THINK C 4.0.2 and Symantec's OOP libraries.
  24.     Portions of this code courtesy Symantec, Inc.
  25.     This code may be freely distributed as long as this notice remains. If you wish
  26.     to distribute modifications to these classes, please make a copy of the class
  27.     and implement your changes there. That's the beauty of OOP!
  28. since:
  29. a) I was not using Symantec's OOP, and
  30. b) CEditor.C had (due to the dependence on the Symantec libraries) a serious limitation,
  31.  to wit a limit of 32,767 pixels of document height (about 3000 lines of text),
  32. eEdit.c is a recasting of CEditor.C in vanilla C with very significant enhancements and fixes.
  33. */
  34.  
  35. /* 10Dec92  e  -- to do:
  36. - better out-of-memory error handling
  37. */
  38.  
  39. /* for arrow keys which ~work like ThinkC editor instead of Apple standard
  40. #define THINK_ARROWS (1)
  41. */
  42. /* for RETURN which auto-tabs like ThinkC editor - not good with Gambit
  43. 16Dec92  e  -- dont turn this on; it doesn't work anymore!
  44. #define THINK_RETURN (1)
  45. */
  46. /* to allow chars > 127 in the text... */
  47. #define CHAR8_OK (1)
  48.  
  49. /* to make Kill (DEL FWD) copy (c-K) vs. delete -- 24Aug94 e */
  50. #define Kill_Deletes (1)
  51.  
  52. /* eEdit documentation...
  53.  
  54.     Limitations
  55.         text size is limited by...
  56.             memory space
  57.               = ~200 + text + (#lines + #style-runs + MIN_LINES + MIN_RUNS) * 4
  58.             32K lines max
  59.             32K style runs max
  60.           eEdit's been tested with files several hundred K in size
  61.           there is no pixel height limit (which constrains many editors to ~3000 lines)
  62.         exactly two styles per text
  63.  
  64.     eTeKey()
  65.         EXTENDED KEYBOARD:
  66.             F1 - F4 - Undo, Cut, Copy, Paste
  67.             F5 - F15 - function keys (not implemented)
  68.             KeyDel - delete forward, erase character after cursor position
  69.             [these next four do not move the insert point, just scroll]
  70.             KeyHome - scrolls text to the top of the screen
  71.             KeyEnd - scrolls text to the bottom of the screen
  72.             KeyPageUp - scrolls text up a page
  73.             KeyPageDown - scrolls text down a page
  74.  
  75.         ALL KEYBOARDS
  76.             LEFT_ARROW - move cursor to the left one character
  77.             RIGHT_ARROW - move cursor to the right one character
  78.             UP_ARROW - move cursor up one line
  79.             DOWN_ARROW - move cursor down one line
  80.             DELETE - erase character before cursor position
  81.             RETURN/ENTER - insert new line and move to left margin (insert a RETURN)
  82.             TAB - insert TAB character; displayed as N spaces
  83.             0x20 - 0xFF - range of valid text characters
  84.  
  85.       Arrow keys can be modified with shift, option, and command
  86.         shift - extend selection per Apple standard (optionally ThinkC mode)
  87.         option - left/right by word, may be used with shift key to extend by word
  88.         option - up/down by page, may be used with shift key to extend by page
  89.         option+shift up/down arrows make the start/end of the selection the active end
  90.         command - left/right: start/end of line, up/down: start/end of text
  91.                     may be used with shift key to extend selection
  92.  
  93.     *** NOTE: 
  94.     *** command-arrow keys will work ONLY IF the main event loop
  95.     *** lets command keys not associated with menu items pass thru to eEdit
  96.  
  97.     eTeClick()
  98.         Mouse clicks
  99.             single - position insert point
  100.             double - select word
  101.             triple - select line
  102.           dragging after one of these extends selection
  103.             by character, word, line repectively; anchor point is original click,
  104.             active end is the other end of the selection
  105.           after drag, active end can be moved with shift clicks or shift arrow keys
  106.           anchor and active ends of selection can be swapped
  107.             with option+shift+click, or option+shift+up/down arrow keys
  108.             
  109.     scrolling: holding down command, shift, option, and control keys
  110.                 speeds up scrolling when mousing in the arrows of the scroll bar
  111.                 speed is 2^^count lines per iteration,
  112.                  where count is a count of the pressed keys
  113. */
  114.  
  115.  
  116. #define OPTION_SPACE    ' '                /* typed as OPTION <SPACE> - used for tabs */
  117. #define EMPTY_PTR        ((Ptr )2L)        /* valid but nonsense ptr used for Munger */
  118. #define PTL(a)           *((long *)&(a))    /* coerce Point or ChPos type to long */
  119.  
  120. #define        Abs(x)            ((x) < 0 ? -(x) : (x))
  121. #define        Max(x, y)        ((x) > (y) ? (x) : (y))
  122. #define        Min(x, y)        ((x) < (y) ? (x) : (y))
  123.  
  124. #define CARET_ON FALSE
  125. #define CARET_OFF TRUE
  126.  
  127. EventRecord            gLastMouseUp;
  128. EventRecord            gLastMouseDown;
  129. long                gMaxSleep;
  130.  
  131. static short         gClicks;
  132. static RgnHandle     gUtilRgn;
  133. static eRec          **gLastViewHit;
  134. static ChPos        zeroPos = { 0, 0 };
  135. /*
  136. #define    MAXINT        32767
  137. static Rect            gMobyRect = { -MAXINT, -MAXINT, MAXINT, MAXINT };
  138. */
  139. TextStyle dfltStylNormal =
  140. { monaco, normal,          0, 9, { 65536, 65536, 65536 } /* RGBColor Black */ };
  141. TextStyle dfltStylHilite =
  142. { monaco, bold+condense,   0, 9, { 65536, 65536, 65536 } /* RGBColor Black */ };
  143.  
  144.  
  145. static void eTeEdGuts( eRec **hE, Ptr textPtr, long numChars,
  146.                          Boolean hasReturn, Boolean show, short style );
  147. static void eTeFontChanged( eRec **hE );
  148. static void eTeHiliteRange( eRec **hE, register ChPos start, register ChPos stop );
  149. static void eTeAdjustScrollMax( eRec **hE );
  150. static void eTeCalibrate( eRec **hE );
  151. static short eTeOffsetToRun( eRec **hE, long anOffset );
  152. static short eTeRunToStyle( eRec **hE, short r );
  153. static ChPos eTePointHtoChPosH( eRec **hE, register Point aPt, register ChPos aPos );
  154. static ChPos eTePointToChPos( eRec **hE, register Point aPt );
  155. static Point eTeChPosToPoint( eRec **hE, register ChPos aPos );
  156. static void eTeDoHscroll( eRec **hE, short whichPart );
  157. static void eTeDoVscroll( eRec **hE, short whichPart );
  158. static Boolean eTeAutoScroll( eRec **hE, Point mouseLoc );
  159. static pascal void hSBarActionProc( ControlHandle macControl, short whichPart );
  160. static pascal void vSBarActionProc( ControlHandle macControl, short whichPart );
  161. /* obsolete...
  162. void     eTeSetFontNumber( eRec **hE, short aFontNumber );
  163. void     eTeSetFontName( eRec **hE, Str255 aFontName );
  164. void     eTeSetFontSize( eRec **hE, short aFontSize );
  165. */
  166. static short eTeUpdateLineStarts( eRec **hE, short firstLine );
  167. static void eTeKeyIns( eRec **hE, char c, short style );
  168.  
  169. /* the scrap  13Aug92  e  */
  170. static Handle eTeScrap;
  171. static long   eTeScrapLen;
  172. static long   eTeScrapCnt;
  173.  
  174. /* undo       13Aug92  e  */
  175.  
  176. typedef struct undoStuff
  177. {    unsigned char *undoTitle;            /* string for Edit menu */
  178.     void  (*undoProc)( eRec  **hE );    /* proc to do undo */
  179.     Handle    undoText;                    /* text that was wiped by last operation */
  180.     long    undoTxLen;                    /* size of undoText */
  181.     long    undoInLen;                    /* number of chars inserted since last delete */
  182.     eRec  **undoTeRec;                    /* eRec       of last operation */
  183.     long    undoStart;                    /* selStart   before last operation */
  184.     long    undoEnd;                    /* selEnd     before last operation */
  185.     Boolean undoDirty;                    /* dirty      before last operation */
  186.     Boolean undoKeyAccum;                /* can accumulate keystrokes */
  187.     Boolean undoDeleting;                /* some version of delete was last operation */
  188.     Boolean undoCarStaP;                /* TRUE if CaretChPos == selStart */
  189. } undoStuff;
  190.  
  191. static undoStuff eTeUndoStuff;
  192.  
  193. unsigned char *utUndo   = "\pUndo";
  194. unsigned char *utCopy   = "\pUndo Copy";
  195. unsigned char *utCut    = "\pUndo Cut";
  196. unsigned char *utPaste  = "\pUndo Paste";
  197. unsigned char *utClear  = "\pUndo Clear";
  198. unsigned char *utTyping = "\pUndo Typing";
  199. unsigned char *utInsert = "\pUndo Insert";
  200. unsigned char *utKill   = "\pUndo Kill";
  201.  
  202. unsigned char *rtCopy   = "\pRedo Copy";
  203. unsigned char *rtCut    = "\pRedo Cut";
  204. unsigned char *rtPaste  = "\pRedo Paste";
  205. unsigned char *rtClear  = "\pRedo Clear";
  206. unsigned char *rtTyping = "\pRedo Typing";
  207. unsigned char *rtInsert = "\pRedo Insert";
  208. unsigned char *rtKill   = "\pRedo Kill";
  209.  
  210. /* DeTabifyHandle takes a text handle,
  211.     and a size (which it assumes is less than or equal to the HandleSize)
  212.      removes all characters in the range 0..0x1f except RETURN (0x0d)
  213.      replacing TAB characters (0x09) with spaces according to tabstop
  214.      it makes two passes over the text,
  215.       and only one resize call to the memory manager
  216.      it returns the number of lines in count (passed by reference)
  217.      and an OsErr result  ( 0 == noErr )
  218. */
  219. /* static */
  220. short DeTabifyHandle( Handle h, long *size, long *count, short tabstop )
  221. { long old_size = *size;
  222.   long new_size = *size;
  223.   long line_count = 0;
  224.   register unsigned char *start = *(unsigned char **)h;
  225.   register unsigned char *end = &start[old_size-1];
  226.   register unsigned char *p = start;
  227.   register unsigned char *q = start;
  228.   register unsigned char c;
  229.   short error;
  230.   char lastch = *end;
  231.   
  232.   *end++ = '\0';
  233.   while ( p < end )
  234.   { c = *p++;
  235.     if ( c < ' ' )
  236.     { if ( c == '\r' )
  237.       { line_count++;
  238.         *q++ = c;
  239.         start = q;
  240.       }
  241.       else if ( c == '\t' )
  242.       { c = tabstop - ( ( q - start ) % tabstop);
  243.         *q++ = c;
  244.         c -= 1;
  245.         start -= c;
  246.         new_size += c;
  247.       }
  248.       else if ( p == end )
  249.       {  *q++ = lastch; /* the last char may be a control char */
  250.       }
  251.       else
  252.       { old_size -= 1;
  253.         new_size -= 1;
  254.       }
  255.     }
  256.     else
  257.       *q++ = c;
  258.   }
  259.   *count = line_count;
  260.   *size = new_size;
  261.   SetHandleSize( h, new_size );
  262.   error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  263.   if ( error == noErr )
  264.   { end = *(unsigned char **)h;
  265.     p = &end[new_size];
  266.     *--p = lastch;
  267.     start = &end[old_size-1];
  268.     while ( p > end )
  269.     { c = *--start;
  270.       if ( c < ' ' && c != '\r' )
  271.         while ( c-- ) *--p = ' ';
  272.       else
  273.         *--p = c;
  274.     }
  275.   }
  276.   else
  277.   { /* cleanup the buffer? */
  278.   }
  279.   return error;
  280. }
  281.  
  282. static void undoBugNi( eRec **hE )
  283. {    SysBeep(3); SysBeep(10); SysBeep(3);
  284. }
  285.  
  286. static ControlActionUPP hSBarActionProcUPP; /* 04Jan95 e */
  287. static ControlActionUPP vSBarActionProcUPP; /* 04Jan95 e */
  288.  
  289. void eTeInit( void )
  290. {
  291.     gUtilRgn = NewRgn();
  292.     gClicks = 0;
  293.     /* 13Aug92  e  */
  294.     eTeScrap = NewHandle(0);
  295.     eTeScrapLen = 0;
  296.     eTeScrapCnt = (InfoScrap())->scrapCount - 1;
  297.     eTeGetScrap();
  298.     /* */
  299.     eTeUndoStuff.undoProc = undoBugNi;
  300.     eTeUndoStuff.undoTitle = utUndo;
  301.     eTeUndoStuff.undoText = NewHandle(0);
  302.     eTeUndoStuff.undoTeRec = NULL;
  303.     hSBarActionProcUPP = NewControlActionProc(hSBarActionProc); /* 04Jan95 e */
  304.     vSBarActionProcUPP = NewControlActionProc(vSBarActionProc); /* 04Jan95 e */
  305. }
  306.  
  307. /* replacement can be installed via eTeSetWordBreak() */
  308.  
  309. static long WordLimits( char *text, long offset, Boolean reverse )
  310. {
  311.     register char *ptr, c;
  312.  
  313.     ptr = text + offset;
  314.     if ( reverse ) {
  315.         /* Scan backwards until we find beginning of word */
  316.         while ( ptr > text ) {
  317.             c = *( ptr - 1 );
  318.             if ( ( c >= 'a' && c <= 'z' ) ||
  319.                  ( c >= 'A' && c <= 'Z' ) || 
  320.                  ( c >= '0' && c <= '9' ) || 
  321.                  ( c == '_' ) )
  322.                 --ptr;
  323.             else
  324.                 break;
  325.         }
  326.     }
  327.     else {
  328.         /* Scan forwards until we find end of word */
  329.         while ( 1 ) {
  330.             c = *ptr;
  331.             if ( ( c >= 'a' && c <= 'z' ) ||
  332.                  ( c >= 'A' && c <= 'Z' ) || 
  333.                  ( c >= '0' && c <= '9' ) || 
  334.                  ( c == '_' ) )
  335.                 ++ptr;
  336.             else
  337.                 break;
  338.         }
  339.     }
  340.     return( ptr - text );
  341. }
  342.  
  343. static short eTeNewRuns( eRec **hE, long len )
  344. {    register long *runPtr;
  345.     register eRec *pE;
  346.     short error;
  347.  
  348.     if ( (**hE).hRuns )
  349.         DisposHandle( (Handle )(**hE).hRuns );
  350.     (**hE).hRuns = (long **)NewHandle( MIN_RUNS * sizeof( long ) );
  351.       error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  352.       if( error == noErr )
  353.       {    pE = *hE;
  354.         runPtr = *((*pE).hRuns);
  355.         *runPtr++ = 0L;
  356.         *runPtr = len;
  357.         (*pE).runsAllocated = MIN_RUNS;
  358.         (*pE).qRuns = 1;
  359.         (*pE).fustStyle = 0;
  360.     }
  361.     return error;
  362. }
  363.  
  364. long eTeTextLength( eRec **hE )
  365. {
  366.     return (*(**hE).hRuns)[(**hE).qRuns];
  367. }
  368.  
  369. eRec **eTeNew( WindowPtr macPort, Rect viewRect, short tabStops, short wrap,
  370.                  short autoInd, ControlHandle aHSizing, ControlHandle aVSizing )
  371. {    
  372.     register eRec *pE;
  373.     LineRec *linePtr;
  374.     eRec   **hE = (eRec **)NewHandle( sizeof( eRec ) );
  375.  
  376.     if( hE != 0 )
  377.     {    HLock( (Handle )hE );
  378.         pE = *hE;
  379.         (*pE).active = FALSE;
  380.         (*pE).macPort = macPort;
  381.         (*pE).viewRect = viewRect;
  382.         (*pE).width = viewRect.right - viewRect.left;
  383.         (*pE).height = viewRect.bottom - viewRect.top;
  384.         (*pE).hOrigin = -viewRect.left;
  385.         (*pE).vOrigin = -viewRect.top;
  386.         (*pE).position.h = (*pE).position.v = 0;
  387.         (*pE).hScale = (*pE).vScale = 1;
  388.         (*pE).bounds.v = 1;
  389.         (*pE).bounds.h = 1;     /* arbitrary size for now */
  390.         (*pE).leftMargin = 1;    /* room for cursor will blink at the far left */
  391.         (*pE).topMargin = 0;
  392.         (*pE).selStart.h = (*pE).selStart.v = 0;
  393.         (*pE).selEnd = (*pE).selStart;
  394.         (*pE).selActive = FALSE;
  395.         (*pE).caretChPos.h = (*pE).caretChPos.v = 0;
  396.         (*pE).writeChPos.h = (*pE).writeChPos.v = 0;
  397.         (*pE).caretState = CARET_OFF;
  398.         (*pE).maxRight = 0;
  399.         (*pE).eTeWordBreak = (ProcPtr )WordLimits; /* ugh, universal headers kludge 7Jun94 e */
  400.         SetPort( macPort );
  401.         (*pE).tabStops = tabStops;
  402.         (*pE).spaceWidth = 1;
  403.         (*pE).wrap = wrap;                            /*  5Jul92  e  */
  404.         (*pE).autoInd = autoInd;
  405.         (*pE).hText = NewHandle( 1L );
  406.         **((*pE).hText) = '\0';
  407.         (*pE).hLines = (LineRec **)NewHandle( MIN_LINES * sizeof( LineRec ) );
  408.         linePtr = *((*pE).hLines);
  409.         *linePtr++ = 0L;
  410.         *linePtr = 0L;
  411.         (*pE).linesAllocated = MIN_LINES;
  412.         /* 2May92  e  */
  413.         (*pE).hRuns = NULL;
  414.         eTeNewRuns( hE, 0L);
  415.         (*pE).qRuns = 0;
  416.         (*pE).hPrint = NULL;                        /* 28Sep92  e  */
  417.         /* scrollPane */
  418.         (*pE).hStep = (*pE).vStep = 1;
  419.         (*pE).hOverlap = (*pE).vOverlap = 1;
  420.         (*pE).hContext = (*pE).vContext = CONTEXT_LINES;
  421.         if ( ( (*pE).hSBar = aHSizing ) != NULL )
  422.         {    SetCRefCon( aHSizing, (long )hE );
  423.             SetCtlAction( aHSizing, hSBarActionProcUPP ); /* 04Jan95 e  was: hSBarActionProc */
  424.             /* SetThumbFunc( aHSizing, SBarThumbFunc ); */
  425.         }
  426.         if ( ( (*pE).vSBar = aVSizing ) != NULL )
  427.         {    SetCRefCon( aVSizing, (long )hE );
  428.             SetCtlAction( aVSizing, vSBarActionProcUPP ); /* 04Jan95 e  was: vSBarActionProc */
  429.             /* SetThumbFunc( aVSizing, SBarThumbFunc ); */
  430.         }
  431.         HUnlock( (Handle )hE );
  432.         eTeSetStyles( hE, &dfltStylNormal, &dfltStylHilite);
  433.         eTeAdjustScrollMax( hE );
  434.         eTeCalibrate( hE );
  435.         (**hE).dirty = FALSE;  /* was (*pE). 14Jul92  e  */
  436.         (**hE).active = TRUE;  /* was (*pE). 14Jul92  e  */
  437.     }
  438.     return hE;
  439. }
  440.  
  441. void eTeDispose( eRec **hE )
  442. {
  443.     if ( (**hE).hText )
  444.         DisposHandle( (**hE).hText );
  445.     (**hE).hText = NULL;
  446.     if ( (**hE).hLines )
  447.         DisposHandle( (Handle )(**hE).hLines );
  448.     (**hE).hLines = NULL;
  449.     if ( (**hE).hRuns )
  450.         DisposHandle( (Handle )(**hE).hRuns );
  451.     (**hE).hRuns = NULL;
  452.     if ( (**hE).hPrint )
  453.         DisposHandle( (Handle)(**hE).hPrint );    /*  28Sep92  e  */
  454.     DisposHandle( (Handle )hE );
  455. }
  456.  
  457. static void eTePrepare( eRec **hE )
  458. {
  459.     Rect        tempRect;                /* ClipRect may move memory    */
  460.  
  461.     SetPort( (**hE).macPort );
  462.     tempRect = (**hE).viewRect;
  463.     ClipRect(&tempRect);
  464.     /* obsolete...
  465.     TextFont( (**hE).fontNumber );
  466.     TextSize( (**hE).fontSize );
  467.     TextFace( 0 ); */
  468.     /* Paranoid... */
  469.     TextMode( srcOr );
  470. }
  471.  
  472. static void eTePrepareStyle( eRec **hE, short style )
  473. {
  474.   if( (**hE).curStyle != style )
  475.   { (**hE).curStyle = style;
  476.     TextFont( (**hE).style[style].tsFont );
  477.     TextFace( (**hE).style[style].tsFace );
  478.     TextSize( (**hE).style[style].tsSize );
  479.     /* RGBColor tsColor; */
  480.   }
  481. }
  482.  
  483. static void eTePrepareRun( eRec **hE, short run )
  484. {
  485.     eTePrepareStyle( hE, eTeRunToStyle( hE, run ) );
  486. }
  487.  
  488. static void eTeRefresh( eRec **hE )
  489. {
  490.     SetPort( (**hE).macPort );
  491.     InvalRect( &(**hE).viewRect );
  492. }
  493.  
  494. static void eTeUpdateCaretRect( eRec **hE )
  495. {
  496.     Point    tempPt;
  497.     register eRec    *pE;
  498.  
  499.     tempPt = eTeChPosToPoint( hE, (**hE).caretChPos );
  500.     pE = *hE;
  501.     (*pE).caretRect.top    = tempPt.v;
  502.     (*pE).caretRect.right  = tempPt.h;
  503.     (*pE).caretRect.left   = tempPt.h - 1;
  504.     (*pE).caretRect.bottom = tempPt.v + (*pE).caretHeight;
  505. }
  506.  
  507. void eTeSetWrap( eRec **hE, short wrap )
  508. {
  509.     /* 22Jul92  e  -- added limits */
  510.     if( wrap < 1 ) wrap = 1;
  511.     else if( wrap > 999 ) wrap = 999;
  512.     /* (**hE).wrap = wrap; */
  513.     /* 10Aug92  e   need to fix all ChPos for new wrap! */
  514.     if( (**hE).wrap != wrap )
  515.     { long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  516.       long sta = eTeChPosToOffset( hE, (**hE).selStart );
  517.       long end = eTeChPosToOffset( hE, (**hE).selEnd );
  518.       long wri = eTeChPosToOffset( hE, (**hE).writeChPos );
  519.       (**hE).wrap = wrap;
  520.       eTeUpdateLineStarts( hE, 0 );
  521.       (**hE).caretChPos = eTeOffsetToChPos( hE, car );
  522.       (**hE).selStart   = eTeOffsetToChPos( hE, sta );
  523.       (**hE).selEnd     = eTeOffsetToChPos( hE, end );
  524.       (**hE).writeChPos = eTeOffsetToChPos( hE, wri );
  525.       eTeUpdateCaretRect( hE );
  526.       eTeRefresh( hE );
  527.     }
  528. }
  529.  
  530. void eTeSetTabStop( eRec **hE, short aTabStop )
  531. {
  532.     /* Undo? */
  533.     (**hE).tabStops = aTabStop;
  534.     (**hE).tabWidth = aTabStop * (**hE).spaceWidth;
  535.     eTeRefresh( hE );
  536. }
  537.  
  538. static short eTeTabStop( eRec **hE, register short curPosition )
  539. {
  540.     register short tabWidth = (**hE).tabWidth;
  541.     return (curPosition +
  542.              (tabWidth -
  543.                ((curPosition - (**hE).leftMargin + (**hE).hOrigin) % tabWidth)));
  544. }
  545.  
  546. void eTeSetWordBreak( eRec **hE, ProcPtr aFunc )
  547. {
  548.     if ( aFunc == NULL )
  549.         (**hE).eTeWordBreak = (ProcPtr )WordLimits; /* ugh, universal headers kludge 7Jun94 e */
  550.     else
  551.         (**hE).eTeWordBreak = aFunc;
  552. }
  553.  
  554. /* 23Jul92  e  */
  555.  
  556. /* Inverts the text between the start and stop positions. */
  557.  
  558. static void eTeHiliteRange( eRec **hE, register ChPos start, register ChPos stop )
  559. {
  560.     Rect tmpRect;
  561.     Point stopPt;
  562.     register short vScale;
  563.     
  564.     if ( ! (**hE).active )
  565.         return;
  566.  
  567.     topLeft( tmpRect ) = eTeChPosToPoint( hE, start );
  568.     tmpRect.left -= 1;
  569.  
  570.     vScale = (**hE).vScale;
  571.  
  572.     /* Just need to hilite within same line */
  573.     if ( start.v == stop.v ) {
  574.         botRight( tmpRect ) = eTeChPosToPoint( hE, stop );
  575.         tmpRect.right -= 1;
  576.         tmpRect.bottom = tmpRect.top + vScale;
  577.         if ( tmpRect.left != tmpRect.right ) {
  578.             eTePrepare( hE );
  579. #ifndef __CONDITIONALMACROS__
  580.             asm { bclr #hiliteBit, HiliteMode }
  581. #else
  582.             LMSetHiliteMode(LMGetHiliteMode() & 0x7F);
  583. #endif
  584.             InvertRect( &tmpRect );
  585.         }
  586.     }
  587.     /* Hilite spans more than one line */
  588.     else
  589.     {    stopPt = eTeChPosToPoint( hE, stop );
  590.         if( tmpRect.top < (**hE).viewRect.bottom && stopPt.v >= (**hE).viewRect.top )
  591.         {    eTePrepare( hE );
  592.             tmpRect.right = (**hE).viewRect.right;
  593.             tmpRect.bottom = tmpRect.top + vScale;
  594.             if( tmpRect.bottom > (**hE).viewRect.top )
  595.             {
  596. #ifndef __CONDITIONALMACROS__
  597.                 asm { bclr #hiliteBit, HiliteMode }
  598. #else
  599.                 LMSetHiliteMode(LMGetHiliteMode() & 0x7F);
  600. #endif
  601.                 InvertRect( &tmpRect );
  602.                 tmpRect.top += vScale;
  603.             }
  604.             else
  605.                 tmpRect.top = (**hE).viewRect.top;
  606.             tmpRect.left = (**hE).viewRect.left;
  607.             tmpRect.bottom = Min( stopPt.v, (**hE).viewRect.bottom );
  608. #ifndef __CONDITIONALMACROS__
  609.             asm { bclr #hiliteBit, HiliteMode }
  610. #else
  611.             LMSetHiliteMode(LMGetHiliteMode() & 0x7F);
  612. #endif
  613.             InvertRect( &tmpRect );
  614.             if( stopPt.v < (**hE).viewRect.bottom )
  615.             {    tmpRect.bottom += vScale;
  616.                 tmpRect.top = stopPt.v;
  617.                 tmpRect.right = stopPt.h - 1;
  618. #ifndef __CONDITIONALMACROS__
  619.                 asm { bclr #hiliteBit, HiliteMode }
  620. #else
  621.                 LMSetHiliteMode(LMGetHiliteMode() & 0x7F); /* ~hiliteBit 4 times 18Jul94 e */
  622. #endif
  623.                 InvertRect( &tmpRect );
  624.             }
  625.         }
  626.     }
  627. }
  628.  
  629. /* 23Jul92  e  */
  630.  
  631. static void eTeSetCaretState( eRec **hE, Boolean state )
  632. {
  633.     Rect    tmpRect;
  634.     register eRec    *pE = *hE;
  635.  
  636.     if (   ! (*pE).selActive
  637.           && (*pE).active
  638.           && (*pE).caretState != state )
  639.     {
  640.         (*pE).caretState = state;
  641.         tmpRect = (*pE).caretRect;
  642.         eTePrepare( hE );
  643.         PenMode(patXor);
  644.         /* FrameRect( &tmpRect ); */
  645.         MoveTo( tmpRect.right, tmpRect.top );
  646.         LineTo( tmpRect.right, tmpRect.bottom );
  647.         /* 13Apr 93  e */
  648.         PenNormal();
  649.     }
  650. }
  651.  
  652. void eTeActivate( eRec **hE )
  653. {
  654.     if( (**hE).active == FALSE )
  655.     {    (**hE).active = TRUE;
  656.         if( (**hE).selActive )
  657.             eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  658.     }
  659. }
  660.  
  661. void eTeDeactivate( eRec **hE )
  662. {
  663.     if( (**hE).active == TRUE )
  664.     {    if( (**hE).selActive )
  665.             eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  666.         else
  667.             eTeSetCaretState( hE, CARET_OFF );
  668.         (**hE).active = FALSE;
  669.     }
  670. }
  671.  
  672. void eTeIdle( eRec **hE )
  673. {
  674.     static long lastCaretToggle = 0L;
  675.     register long    now;
  676.     Rect    tmpRect;
  677.  
  678.     gMaxSleep = GetCaretTime();    /* user may change it using desk accessory */
  679.     now = TickCount();
  680.     if ( now - lastCaretToggle >= gMaxSleep ) {
  681.         lastCaretToggle = now;
  682.         eTeSetCaretState( hE, (**hE).caretState == CARET_OFF ? CARET_ON : CARET_OFF );
  683.     }
  684.     else {
  685.         gMaxSleep -= ( now - lastCaretToggle );
  686.     }
  687. }
  688.  
  689. static void eTeResize( eRec **hE, register Rect *delta )
  690. {
  691.     register eRec *pE = *hE;
  692.  
  693.     (*pE).width      += delta->right - delta->left;
  694.     (*pE).height     += delta->bottom - delta->top;
  695.     (*pE).leftMargin += delta->left;
  696.     (*pE).topMargin  += delta->top;
  697.  
  698.     (*pE).hOrigin -= delta->left;
  699.     (*pE).vOrigin -= delta->top;
  700.  
  701.     eTeUpdateCaretRect( hE );
  702.     eTeAdjustScrollMax( hE );
  703.     eTeCalibrate( hE );
  704. }
  705.  
  706. void eTeNewView( eRec **hE, Rect *viewRect )
  707. {
  708.     register eRec *pE = *hE;
  709.     Rect    delta;
  710.     
  711.     delta = *viewRect;
  712.     delta.top -= (*pE).viewRect.top;
  713.     delta.left -= (*pE).viewRect.left;
  714.     delta.bottom -= (*pE).viewRect.bottom;
  715.     delta.right -= (*pE).viewRect.right;
  716.     (*pE).viewRect   = *viewRect;
  717.     
  718.     eTeResize( hE, &delta );
  719. }
  720.  
  721. /* eTeSetStyles also causes the display to be redrawn */
  722.  
  723. void eTeSetStyles( eRec **hE, TextStyle *ts0, TextStyle *ts1 )
  724. {
  725.     short        height[2];
  726.     FontInfo    fontInfo[2];
  727.     ChPos        tmpPt;
  728.     short        bigger;
  729.  
  730.     (**hE).style[0] = *ts0;
  731.     (**hE).style[1] = *ts1;
  732.     (**hE).curStyle = NOSTYLE;
  733.  
  734.     /* Turn off cursor */
  735.     eTePrepare( hE );
  736.     eTeSetCaretState( hE, CARET_OFF );
  737.  
  738.     eTePrepareStyle( hE, 1 );
  739.     GetFontInfo( &fontInfo[1] );
  740.     height[1] = CharWidth( OPTION_SPACE );
  741.  
  742.     eTePrepareStyle( hE, 0 );
  743.     GetFontInfo( &fontInfo[0] );
  744.     height[0] = CharWidth( OPTION_SPACE );
  745.     
  746.     (**hE).spaceWidth = Max ( height[0], height[1] );
  747.     /* calculate new tabstop based on new font/size */
  748.     (**hE).tabWidth = (**hE).tabStops * (**hE).spaceWidth;
  749.  
  750.     height[0] = fontInfo[0].ascent + fontInfo[0].descent;
  751.     height[1] = fontInfo[1].ascent + fontInfo[1].descent;
  752.     
  753.     bigger = height[1] > height[0] ? 1 : 0;
  754.     (**hE).fontAscent = fontInfo[bigger].ascent;
  755.     (**hE).caretHeight = height[bigger];
  756.  
  757.     /* Update caret values for new font */
  758.     eTeUpdateCaretRect( hE );
  759.     (**hE).maxRight = (**hE).caretRect.right;
  760.  
  761.     /*
  762.      * Set the scale values for our scrollbars. Vertical scrolling is done by 
  763.      * lines; horizontal by widest character. First, move to top of panorama (but
  764.      * don't redraw), then change the scale values. Finally, move back to original
  765.      * position and redraw screen.
  766.      */
  767.     tmpPt = (**hE).position;
  768.     eTeScroll( hE, -(**hE).position.h, -(**hE).position.v, FALSE );
  769.     (**hE).hScale = Max( fontInfo[0].widMax, fontInfo[1].widMax );
  770.     (**hE).vScale = (**hE).caretHeight + fontInfo[bigger].leading;
  771.     eTeAdjustScrollMax( hE );
  772.     eTeScrollTo( hE, tmpPt, FALSE );
  773.     eTeRefresh( hE );
  774. }
  775.  
  776. #ifdef include_obsolete_code
  777.  
  778. void eTeSetFontNumber( eRec **hE, short aFontNumber )
  779. {
  780.     Str255        fontName;
  781.  
  782.     GetFontName( aFontNumber, fontName );
  783.     if ( fontName[0] == 0 )
  784.         aFontNumber = 0;    /* If font does not exist, then use the system font */
  785.  
  786.     /* Undo? */
  787.     (**hE).fontNumber = aFontNumber;
  788.     eTeFontChanged( hE );
  789. }
  790.  
  791. void eTeSetFontName( eRec **hE, Str255 aFontName )
  792. {
  793.     short    fontNumber;
  794.  
  795.     GetFNum( aFontName, &fontNumber );
  796.     eTeSetFontNumber( hE, ( fontNumber > 0 ) ? fontNumber : 0 );
  797. }
  798.  
  799. void eTeSetFontSize( eRec **hE, short aFontSize )
  800. {
  801.     /* Undo? */
  802.     (**hE).fontSize = aFontSize;
  803.     eTeFontChanged( hE );
  804. }
  805.  
  806. #endif
  807.  
  808. /* static --  25Sep92  e  for os_mac_Print.c */
  809. void eTeDrawLine( eRec **hE, ChPos beginPos, Point location )
  810. {
  811.     register char    c, *charPtr, *firstChar;
  812.     register short    count;
  813.     long     offset, instyle, inlyne, eol;
  814.     short     run, style;
  815.     Point    pt;
  816.  
  817.     HLock( (**hE).hText );
  818.     MoveTo( location.h, location.v + (**hE).fontAscent );
  819.     
  820.     offset = *(*(**hE).hLines + beginPos.v) + beginPos.h;
  821.     charPtr = *(**hE).hText + offset;
  822.     eol    = *(*(**hE).hLines + beginPos.v + 1);
  823.     inlyne = eol - offset;
  824.     run = eTeOffsetToRun( hE, offset );
  825.     while( inlyne )
  826.     {    eTePrepareRun( hE, run );
  827.         instyle = (*(**hE).hRuns)[++run] - offset;
  828.         if( instyle > inlyne )
  829.             instyle = inlyne;
  830.         inlyne -= instyle;
  831.         offset += instyle;
  832.         firstChar = charPtr;
  833.         count = 0;
  834.         while ( instyle && ( c = *charPtr++ ) != RETURN )    /* 11Aug92  e   was: instyle-- */
  835.         {    if ( c == TAB ) 
  836.             {    if ( count > 0 ) DrawText( firstChar, 0, count );
  837.                 GetPen( &pt );
  838.                 pt.h = eTeTabStop( hE, pt.h );
  839.                 MoveTo( pt.h, pt.v );
  840.                 firstChar = charPtr;
  841.                 count = 0;
  842.             }
  843.             else ++count;
  844.             instyle -= 1;                                    /* 11Aug92  e  */
  845.         }
  846.         if ( count  > 0 ) DrawText( firstChar, 0, count );
  847.     }
  848.     if( c != RETURN && *charPtr != '\0' ) DrawChar( 0xd7 );
  849.     /* if( instyle == 0 && *charPtr != '\0' ) DrawChar( 0xd7 ); /* NG!? 11Aug92  e  */
  850.     HUnlock( (**hE).hText );
  851. }
  852.  
  853. void eTeDraw( eRec **hE, Rect *area )
  854. {
  855.     eRec *pE;
  856.     short    vFirst, vLast;
  857.     ChPos    startPos;
  858.     Point    location;
  859.     Rect    tRect;
  860.     Boolean doCaret;    /* 23Jul92  e */
  861.     
  862.     HLock( (Handle )hE );
  863.     pE = *hE;
  864.     
  865.     if ( SectRect( area, &(*pE).viewRect, &tRect ) )
  866.     {
  867.         eTePrepare( hE );
  868.         
  869.         doCaret = ( ( ! (*pE).selActive ) && ( (*pE).caretState == CARET_ON ) );
  870.         if( doCaret ) eTeSetCaretState( hE, CARET_OFF );
  871.  
  872.         vFirst = ( tRect.top - (*pE).topMargin + (*pE).vOrigin ) / (*pE).vScale;
  873.         vLast = ( tRect.bottom + (*pE).vScale - (*pE).topMargin + (*pE).vOrigin - 1 )
  874.                 / (*pE).vScale;
  875.  
  876.         if ( vFirst < 0 )
  877.             vFirst = 0;
  878.         if ( vLast >= (*pE).bounds.v ) 
  879.             vLast = (*pE).bounds.v - 1;
  880.  
  881.         EraseRect( &tRect );
  882.  
  883.         if ( vFirst < (*pE).bounds.v && vLast >= 0)
  884.  
  885.         {    location.h = (*pE).leftMargin - (*pE).hOrigin;
  886.             location.v = vFirst * (*pE).vScale + (*pE).topMargin - (*pE).vOrigin;
  887.             startPos.h = 0;
  888.  
  889.             for ( startPos.v = vFirst;
  890.                   startPos.v <= vLast;
  891.                   location.v += (*pE).vScale, ++startPos.v )
  892.             {    eTeDrawLine( hE, startPos, location );
  893.             }
  894.             if ( (*pE).selActive )
  895.                  eTeHiliteRange( hE, (*pE).selStart, (*pE).selEnd );    /* may have munged it */
  896.             else if( doCaret )
  897.                 eTeSetCaretState( hE, CARET_ON );
  898.         }
  899.     }
  900.     HUnlock( (Handle )hE );
  901. }
  902.  
  903. void eTeUpdate( eRec **hE )
  904. {
  905.     Rect tempRect;
  906.     
  907.     eTePrepare( hE );
  908.     tempRect = (**((**hE).macPort)->visRgn).rgnBBox;
  909.     eTeDraw( hE, &tempRect );
  910. }
  911.         
  912. #ifndef THINK_ARROWS
  913.  
  914. static void eTeExtSelGuts( eRec **hE, ChPos chPos )
  915. {
  916.     if( PTL( chPos ) < PTL( (**hE).caretChPos ) )
  917.     {    eTeHiliteRange( hE, chPos, (**hE).caretChPos );
  918.         if( PTL( (**hE).caretChPos ) == PTL( (**hE).selStart ) )
  919.             (**hE).selStart = chPos;
  920.         else if( PTL( chPos ) >= PTL( (**hE).selStart ) )
  921.             (**hE).selEnd = chPos;
  922.         else
  923.         {    (**hE).selEnd = (**hE).selStart;
  924.             (**hE).selStart = chPos;
  925.         }
  926.     }
  927.     else if( PTL( chPos ) > PTL( (**hE).caretChPos ) )
  928.     {    eTeHiliteRange( hE, (**hE).caretChPos, chPos );
  929.         if( PTL( (**hE).caretChPos ) == PTL( (**hE).selEnd ) )
  930.             (**hE).selEnd = chPos;
  931.         else if( PTL( chPos ) <= PTL( (**hE).selEnd ) )
  932.             (**hE).selStart = chPos;
  933.         else
  934.         {    (**hE).selStart = (**hE).selEnd;
  935.             (**hE).selEnd = chPos;
  936.         }
  937.     }
  938. }
  939.  
  940. #endif
  941.  
  942. static void eTeEnsureChPos( eRec **hE, ChPos chPos )
  943. {
  944.     if( PTL( chPos ) != PTL( (**hE).caretChPos ) )
  945.     {    (**hE).caretChPos = chPos;
  946.         eTeUpdateCaretRect( hE );
  947.         (**hE).maxRight = (**hE).caretRect.right;
  948.     }
  949. }
  950.  
  951. void eTeKey( eRec **hE, char theChar, Byte keyCode, short modifiers, short style )
  952. {
  953.     char        *charPtr;
  954.     LineRec        *linePtr;
  955.     long        offset;
  956.     short            eoln;
  957.     ChPos        chPos;
  958.     char        chars[ 255 ];
  959.     Boolean        shifted, optioned, commanded;
  960.     Rect         invalRect;
  961.     long        new;
  962.     Point         aPt;
  963.  
  964.     ObscureCursor();
  965.     eTeSetCaretState( hE, CARET_OFF ); /* move up here 22Jul92  e  */
  966.  
  967.     shifted = ( modifiers & shiftKey ) ? 1 : 0;
  968.     optioned = ( modifiers & optionKey ) ? 1 : 0;
  969.     commanded = ( modifiers & cmdKey ) ? 1 : 0;
  970.  
  971.     switch ( keyCode ) {
  972.         case KeyF1:
  973.             /* Undo */
  974.             break;
  975.         case KeyF2:
  976.             eTeCut( hE );
  977.             break;
  978.         case KeyF3:
  979.             eTeCopy( hE );
  980.             break;
  981.         case KeyF4:
  982.             /* eTePaste( hE, style );            /* correct        3May92  e */
  983.             eTePaste( hE, style ^ optioned );    /* for debugging  3May92  e */
  984.             break;
  985.         case KeyF5:
  986.         case KeyF6:
  987.         case KeyF7:
  988.         case KeyF8:
  989.         case KeyF9:
  990.         case KeyF10:
  991.         case KeyF11:
  992.         case KeyF12:
  993.         case KeyF13:
  994.         case KeyF14:
  995.         case KeyF15:
  996.             /* DoFunctionKey( theChar, keyCode, modifiers ); */
  997.             break;
  998.  
  999.         case KeyHome:                                            /*** HOME ***/
  1000.             eTeScrollTo( hE, zeroPos, TRUE );
  1001.             break;
  1002.  
  1003.         case KeyEnd:                                            /*** END ***/
  1004.             chPos.v = (**hE).bounds.v - (**hE).vSpan;
  1005.             if ( chPos.v < 0 )
  1006.                 chPos.v = 0;
  1007.             chPos.h = (**hE).position.h;
  1008.             eTeScrollTo( hE, chPos, TRUE );
  1009.             break;
  1010.  
  1011.         case KeyPageUp:                                            /*** PAGE UP ***/
  1012.             eTeDoVscroll( hE, inPageUp );
  1013.             eTeAdjustScrollMax( hE );
  1014.             break;
  1015.  
  1016.         case KeyPageDown:                                        /*** PAGE DOWN ***/
  1017.             eTeDoVscroll( hE, inPageDown );
  1018.             eTeAdjustScrollMax( hE );
  1019.             break;
  1020.  
  1021.         case KeyDel:                                            /*** DEL FWD ***/
  1022.             offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  1023.             if ( (**hE).selActive )
  1024.                 eTeDelete( hE );
  1025.             else if ( optioned ) { /* delete to end of line */
  1026.                 /* 5Jul92  e */
  1027.                 chPos = (**hE).caretChPos;
  1028.                 linePtr = *(**hE).hLines;
  1029.               labelTryAgainK:
  1030.                 chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  1031.                 if ( chPos.v < (**hE).bounds.v - 1 )
  1032.                 { --chPos.h;
  1033.                   new = eTeChPosToOffset( hE, chPos );
  1034.                   if( *(*(**hE).hText + new) != RETURN )
  1035.                   { chPos.v++;
  1036.                     goto labelTryAgainK;
  1037.                   }
  1038.                   if( new == offset )
  1039.                       chPos.h++;    /* delete the RETURN if its all there is */
  1040.                 }
  1041.                 eTeKillTo( hE, chPos );
  1042.             }
  1043.             else if ( *(*(**hE).hText + offset) != '\0' ) {
  1044.                 /* 14Aug92  e   for undo...
  1045.                 eTeEdGuts( hE, EMPTY_PTR, 0L, *(*(**hE).hText + offset) == RETURN, TRUE, style );
  1046.                 */
  1047.                 eTeKillTo( hE, eTeOffsetToChPos( hE, offset + 1 ) ); /* optimize for Undo Typing */
  1048.             }
  1049.             break;
  1050.  
  1051.         default:
  1052.             MoveHHi( (Handle )(**hE).hLines );
  1053.             HLock( (Handle )(**hE).hLines );
  1054.             /* eTeSetCaretState( hE, CARET_OFF );  --  move above  22Jul92  e  */
  1055.             linePtr = *(**hE).hLines;
  1056.             switch( theChar ) {
  1057.                 case LEFT_ARROW:                                /*** LEFT ARROW ***/
  1058.                     if ( (**hE).selActive && ! shifted )
  1059.                     {    /* if no shift key and there is a selection, remove it */
  1060.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1061.                         chPos = (**hE).selEnd = (**hE).selStart;
  1062.                     }
  1063. #ifndef THINK_ARROWS
  1064.                     else if( (**hE).selActive && optioned && commanded )
  1065.                     {    /* move chPos to start of selection if not there */
  1066.                         chPos = (**hE).selStart;
  1067.                         eTeEnsureChPos( hE, chPos );
  1068.                     }
  1069. #endif
  1070.                     else
  1071.                     {    if ( shifted )
  1072.                           {    if ( ! (**hE).selActive )
  1073.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1074. #ifdef THINK_ARROWS
  1075.                             (**hE).caretChPos = (**hE).selStart;
  1076. #endif
  1077.                         }
  1078.                         chPos = (**hE).caretChPos;
  1079.                         if ( commanded )
  1080.                         {    chPos.h = 0;    /* move to beginning of line */
  1081.                             labelTryAgainL:    /* added the rest 5Jul92  e  */
  1082.                             if ( chPos.v > 0 )
  1083.                             {    offset = eTeChPosToOffset( hE, chPos );
  1084.                                 if( *(*(**hE).hText + offset - 1) != RETURN )
  1085.                                 {    chPos.v--;
  1086.                                     goto labelTryAgainL;
  1087.                                 }
  1088.                             }
  1089.                         }
  1090.                         else if ( optioned )
  1091.                         {                     /* move to start/end of previous word */
  1092.                               offset = eTeChPosToOffset( hE, chPos );
  1093.                             new = WordLimits( *(**hE).hText, offset, TRUE );
  1094.                             if ( new == offset && offset != 0L )
  1095.                                 --new;
  1096.                             chPos = eTeOffsetToChPos( hE, new );
  1097.                         }
  1098.                           else                 /* move to previous character position */
  1099.                             chPos = eTeOffsetToChPos( hE, eTeChPosToOffset( hE, chPos ) - 1 );
  1100.                         if ( shifted )
  1101.                         {
  1102. #ifdef THINK_ARROWS
  1103.                             eTeHiliteRange( hE, chPos, (**hE).selStart );
  1104.                             (**hE).selStart = chPos;
  1105. #else
  1106.                             eTeExtSelGuts( hE, chPos );
  1107. #endif
  1108.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1109.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1110.                         }
  1111.                     }
  1112.                     (**hE).caretChPos = chPos;
  1113.                     eTeUpdateCaretRect( hE );
  1114.                     (**hE).maxRight = (**hE).caretRect.right;
  1115.                     eTeShowCaret( hE );
  1116.                     break;
  1117.  
  1118.                 case RIGHT_ARROW:                                /*** RIGHT ARROW ***/
  1119.                     if ( (**hE).selActive && ! shifted )
  1120.                     {    /* if no shift key and there is a selection, remove it */
  1121.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1122.                         chPos = (**hE).selStart = (**hE).selEnd;
  1123.                     }
  1124. #ifndef THINK_ARROWS
  1125.                     else if( (**hE).selActive && optioned && commanded )
  1126.                     {    /* move chPos to end of selection if not there */
  1127.                         chPos = (**hE).selEnd;
  1128.                         eTeEnsureChPos( hE, chPos );
  1129.                     }
  1130. #endif
  1131.                     else
  1132.                     {    if ( shifted )
  1133.                           {    if ( ! (**hE).selActive )
  1134.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1135. #ifdef THINK_ARROWS
  1136.                             (**hE).caretChPos = (**hE).selEnd;
  1137. #endif
  1138.                         }
  1139.                         chPos = (**hE).caretChPos;
  1140.                         if ( commanded )
  1141.                         {                    /* move to end of line */
  1142.                             labelTryAgainR:
  1143.                             chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  1144.                             if ( chPos.v < (**hE).bounds.v - 1 )
  1145.                             {    /* 5Jul92  e  was: --chPos.h; */
  1146.                                 --chPos.h;
  1147.                                 offset = eTeChPosToOffset( hE, chPos );
  1148.                                 if( *(*(**hE).hText + offset) != RETURN )
  1149.                                 {    chPos.v++;
  1150.                                     goto labelTryAgainR;
  1151.                                 }
  1152.                             }
  1153.                         }
  1154.                         else if ( optioned )
  1155.                         {                     /* move to start/end of next word */
  1156.                               offset = eTeChPosToOffset( hE, chPos );
  1157.                             new = WordLimits( *(**hE).hText, offset, FALSE );
  1158.                             if ( new == offset && *(*(**hE).hText + offset) != '\0' )
  1159.                                 ++new;
  1160.                             chPos = eTeOffsetToChPos( hE, new );
  1161.                         }
  1162.                           else                 /* move to previous character position */
  1163.                             chPos = eTeOffsetToChPos( hE, eTeChPosToOffset( hE, chPos ) + 1 );
  1164.                         if ( shifted )
  1165.                         {
  1166. #ifdef THINK_ARROWS
  1167.                             eTeHiliteRange( hE, (**hE).selEnd, chPos );
  1168.                             (**hE).selEnd = chPos;
  1169. #else
  1170.                             eTeExtSelGuts( hE, chPos );
  1171. #endif
  1172.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1173.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1174.                         }
  1175.                     }
  1176.                     (**hE).caretChPos = chPos;
  1177.                     eTeUpdateCaretRect( hE );
  1178.                     (**hE).maxRight = (**hE).caretRect.right;
  1179.                     eTeShowCaret( hE );
  1180.                     break;
  1181.  
  1182.                 case UP_ARROW:                                /*** UP ARROW ***/
  1183.                     if ( (**hE).selActive && ! shifted )
  1184.                     {    /* if no shift key and there is a selection, remove it */
  1185.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1186.                         chPos = (**hE).selEnd = (**hE).selStart;
  1187.                     }
  1188. #ifndef THINK_ARROWS
  1189.                     else if( (**hE).selActive && optioned && commanded )
  1190.                     {    /* move chPos to start of selection if not there */
  1191.                         chPos = (**hE).selStart;
  1192.                         eTeEnsureChPos( hE, chPos );
  1193.                     }
  1194. #endif
  1195.                     else
  1196.                     {    if ( shifted )
  1197.                           {    if ( ! (**hE).selActive )
  1198.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1199. #ifdef THINK_ARROWS
  1200.                             else
  1201.                                 eTeEnsureChPos( hE, (**hE).selStart );
  1202. #endif
  1203.                         }
  1204.                         chPos = (**hE).caretChPos;
  1205.                         if ( commanded )
  1206.                             chPos.h = chPos.v = 0;    /* move to beginning of text */
  1207.                         else if( optioned )
  1208.                         {    aPt.h = (**hE).maxRight;
  1209.                             chPos.v -= (**hE).vSpan - (**hE).vOverlap;    /* move up one page */
  1210.                             if( chPos.v < 0 ) chPos.v = 0;
  1211.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1212.                         }
  1213.                           else if ( chPos.v > 0 )
  1214.                         {    /* NG...   [caretRect.top can be bogus!]
  1215.                             aPt.h = (**hE).maxRight;
  1216.                             aPt.v = (**hE).caretRect.top - (**hE).vScale;
  1217.                             chPos = eTePointToChPos( hE, aPt );
  1218.                             */
  1219.                             aPt.h = (**hE).maxRight;
  1220.                             chPos.v -= 1;    /* move up one line */
  1221.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1222.                         }
  1223.                         if ( shifted )
  1224.                         {
  1225. #ifdef THINK_ARROWS
  1226.                             eTeHiliteRange( hE, chPos, (**hE).selStart );
  1227.                             (**hE).selStart = chPos;
  1228. #else
  1229.                             eTeExtSelGuts( hE, chPos );
  1230. #endif
  1231.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1232.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1233.                         }
  1234.                     }
  1235.                     (**hE).caretChPos = chPos;
  1236.                     eTeUpdateCaretRect( hE );
  1237.                     eTeShowCaret( hE );
  1238.                     break;
  1239.  
  1240.                 case DOWN_ARROW:                            /*** DOWN ARROW ***/
  1241.                     if ( (**hE).selActive && ! shifted )
  1242.                     {    /* if no shift key and there is a selection, remove it */
  1243.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1244.                         chPos = (**hE).selStart = (**hE).selEnd;
  1245.                     }
  1246. #ifndef THINK_ARROWS
  1247.                     else if( (**hE).selActive && optioned && commanded )
  1248.                     {    /* move chPos to end of selection if not there */
  1249.                         chPos = (**hE).selEnd;
  1250.                         eTeEnsureChPos( hE, chPos );
  1251.                     }
  1252. #endif
  1253.                     else
  1254.                     {    if ( shifted )
  1255.                           {    if ( ! (**hE).selActive )
  1256.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1257. #ifdef THINK_ARROWS
  1258.                             else
  1259.                                 eTeEnsureChPos( hE, (**hE).selEnd );
  1260. #endif
  1261.                         }
  1262.                         chPos = (**hE).caretChPos;
  1263.                         if ( commanded )
  1264.                         {                    /* move to end of text */
  1265.                             chPos.v = (**hE).bounds.v - 1;
  1266.                             chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  1267.                         }
  1268.                         else if( optioned )
  1269.                         {    aPt.h = (**hE).maxRight;
  1270.                             if( chPos.v > (**hE).bounds.v - (**hE).vSpan + (**hE).vOverlap )
  1271.                                 chPos.v = (**hE).bounds.v - 1;
  1272.                             else
  1273.                                 chPos.v += (**hE).vSpan - (**hE).vOverlap;    /* move down one page */
  1274.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1275.                         }
  1276.                           else if ( chPos.v < (**hE).bounds.v - 1 )
  1277.                         {    /* NG...   [caretRect.top can be bogus!]
  1278.                             aPt.h = (**hE).maxRight;
  1279.                             aPt.v = (**hE).caretRect.top + (**hE).vScale;
  1280.                             chPos = eTePointToChPos( hE, aPt );
  1281.                             */
  1282.                             aPt.h = (**hE).maxRight;
  1283.                             chPos.v += 1;    /* move down one line */
  1284.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1285.                         }
  1286.                         if ( shifted )
  1287.                         {
  1288. #ifdef THINK_ARROWS
  1289.                             eTeHiliteRange( hE, (**hE).selEnd, chPos );
  1290.                             (**hE).selEnd = chPos;
  1291. #else
  1292.                             eTeExtSelGuts( hE, chPos );
  1293. #endif
  1294.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1295.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1296.                         }
  1297.                     }
  1298.                     (**hE).caretChPos = chPos;
  1299.                     eTeUpdateCaretRect( hE );
  1300.                     eTeShowCaret( hE );
  1301.                     break;
  1302.  
  1303.                 case DELETE:                                /*** DELETE ***/
  1304.                     eTeDelete( hE );
  1305.                     break;
  1306.  
  1307.                 default:                                    /*** TEXT ***/
  1308.                     /* Undo? */
  1309.                     if ( theChar == ENTER || theChar == RETURN ) {
  1310.                         chars[ 0 ] = RETURN;
  1311.                         offset = 1;
  1312. #ifdef THINK_RETURN
  1313.                         if ( (**hE).caretChPos.v > 0 ) {
  1314.                             charPtr = *(**hE).hText + linePtr[ (**hE).caretChPos.v - 1 ];
  1315.                             while ( *charPtr == TAB || *charPtr == SPACE ) {
  1316.                                 chars[ offset++ ] = *charPtr++;
  1317.                             }
  1318.                         }
  1319. #endif
  1320.                     }
  1321. #ifdef CHAR8_OK
  1322.                     else if ( theChar == TAB || (unsigned char)theChar >= SPACE )
  1323. #else
  1324.                     else if ( theChar == TAB ||                theChar >= SPACE )
  1325. #endif
  1326.                     {
  1327.                         chars[0] = theChar;
  1328.                         offset = 1;
  1329.                     }
  1330.                     else {
  1331.                         break;
  1332.                     }
  1333.                     /* Insert character to right of cursor position */
  1334.                     /*  14Aug92  e  for undo...
  1335.                     eTeEdGuts( hE, chars, (long )offset,
  1336.                                  ( chars[0] == RETURN ? TRUE : FALSE ), TRUE, style );
  1337.                     */
  1338.                     eTeKeyIns( hE, chars[0], style );
  1339.                     /* (**hE).maxRight = (**hE).caretRect.right;
  1340.                        -- done in eTeEdGuts --                   02Oct92  e  */
  1341.                     break;
  1342.             } /* END SWITCH ASCII CHARACTER */
  1343.  
  1344.             /* Update selection flag and turn cursor back on */
  1345.             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1346.             eTeSetCaretState( hE, CARET_ON );
  1347.             HUnlock( (Handle )(**hE).hLines );
  1348.             break;
  1349.     }
  1350. }
  1351.  
  1352. void eTeClick( eRec **hE, Point hitPt, short modifierKeys, long when )
  1353. {
  1354.     ChPos    pos;
  1355.     Point    newPt;
  1356.     Boolean didScroll = TRUE;
  1357.     long    low, high, offset, start, end, anchorStart, anchorEnd;
  1358.     eRec *pE;
  1359.     
  1360.     HLock( (Handle )hE );
  1361.     pE = *hE;
  1362.  
  1363.     if ( ( hE == gLastViewHit )  &&
  1364.          ( ( when - gLastMouseUp.when ) < GetDblTime() )  &&
  1365.          ( Abs ( gLastMouseDown.where.h - hitPt.h ) < 3 ) &&
  1366.          ( Abs ( gLastMouseDown.where.v - hitPt.v ) < 3 ) )
  1367.         gClicks++;
  1368.     else
  1369.         gClicks = 1;
  1370.     gLastViewHit = hE;
  1371.  
  1372.     eTePrepare( hE );
  1373.  
  1374.     eTeSetCaretState( hE, CARET_OFF );
  1375.  
  1376.     if ( gClicks > 3 )
  1377.         gClicks = 3;
  1378.  
  1379.     if ( modifierKeys & shiftKey ) {
  1380.         gClicks = 1;
  1381.         if ( ! (*pE).selActive ) {
  1382.             (*pE).selStart = (*pE).caretChPos;
  1383.             (*pE).selEnd = (*pE).selStart;
  1384.             (*pE).selActive = TRUE;
  1385.         }
  1386.         start = eTeChPosToOffset( hE, (*pE).selStart );
  1387.         end = eTeChPosToOffset( hE, (*pE).selEnd );
  1388.         /*  24Jul92  e  was...
  1389.         if( modifierKeys & optionKey )
  1390.             anchorStart = anchorEnd = end;
  1391.         else
  1392.             anchorStart = anchorEnd = start;
  1393.         s.b.... */
  1394.         if( eTeChPosToOffset( hE, (*pE).caretChPos ) == start )
  1395.             anchorStart = anchorEnd = (long )( ( modifierKeys & optionKey ) ? start : end );
  1396.         else
  1397.             anchorStart = anchorEnd = (long )( ( modifierKeys & optionKey ) ? end : start );
  1398.         /* since caretChPos is the active end    24Jul92  e  */
  1399.     }
  1400.     else if ( (*pE).selActive ) {
  1401.         eTeHiliteRange( hE, (*pE).selStart, (*pE).selEnd );
  1402.         (*pE).selActive = FALSE;
  1403.     }
  1404.  
  1405.     /*
  1406.      * The DO-WHILE loop monitors the cursor while the mouse button is held down.
  1407.      * The selection is modified with changes in the cursor. NewPt is the new
  1408.      * mouse location, while hitPt contains the last position.
  1409.      */
  1410.     newPt = hitPt;
  1411.     do {
  1412.         if ( didScroll || PTL( newPt ) != PTL( hitPt ) ) {
  1413.             /* first time OR did scroll last loop OR mouse has moved */
  1414.             /* (didScroll == TRUE) causes this to execute first time */
  1415.             switch ( gClicks ) {
  1416.                 case 3:
  1417.                     pos = eTePointToChPos( hE, newPt );
  1418.                     pos.h = 0;
  1419.                     low = eTeChPosToOffset( hE, pos );
  1420.                     if ( pos.v < (*pE).bounds.v - 1 ) {
  1421.                         ++pos.v;
  1422.                         pos.h = 0;
  1423.                     }
  1424.                     else {
  1425.                         pos.h = (*(*pE).hLines)[ pos.v + 1 ] - (*(*pE).hLines)[ pos.v ];
  1426.                     }
  1427.                     high = eTeChPosToOffset( hE, pos );
  1428.                     break;
  1429.  
  1430.                 case 2:
  1431.                     offset = eTeChPosToOffset( hE, eTePointToChPos( hE, newPt ) );
  1432.                     low = WordLimits( *(*pE).hText, offset, TRUE );
  1433.                     high = WordLimits( *(*pE).hText, offset, FALSE );
  1434.                     break;
  1435.  
  1436.                 default:
  1437.                     low = eTeChPosToOffset( hE, eTePointToChPos( hE, newPt ) );
  1438.                     high = low;
  1439.                     break;
  1440.             }
  1441.             /*
  1442.              * Use selActive variable to tell us if this is the first time thru.
  1443.              * If so, then set our anchor points.
  1444.              * Otherwise we just update the selection around the anchors.
  1445.              */
  1446.             if ( ! (*pE).selActive ) {
  1447.                 start = anchorStart = low;
  1448.                 end = anchorEnd = high;
  1449.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, low ), eTeOffsetToChPos( hE, high ) );
  1450.                 (*pE).selActive = TRUE;
  1451.             }
  1452.             if ( low < start ) {
  1453.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, low ), eTeOffsetToChPos( hE, start ) );
  1454.                 start = low;
  1455.             }
  1456.             else if ( low < anchorStart ) {
  1457.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, start ), eTeOffsetToChPos( hE, low ) );
  1458.                 start = low;
  1459.             }
  1460.             else {
  1461.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, start ), eTeOffsetToChPos( hE, anchorStart ) );
  1462.                 start = anchorStart;
  1463.             }
  1464.             if ( high > end ) {
  1465.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, end ), eTeOffsetToChPos( hE, high ) );
  1466.                 end = high;
  1467.             }
  1468.             else if ( high > anchorEnd ) {
  1469.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, high ), eTeOffsetToChPos( hE, end ) );
  1470.                 end = high;
  1471.             }
  1472.             else {
  1473.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, anchorEnd ), eTeOffsetToChPos( hE, end ) );
  1474.                 end = anchorEnd;
  1475.             }
  1476.             (*pE).selStart = eTeOffsetToChPos( hE, start );
  1477.             (*pE).selEnd = eTeOffsetToChPos( hE, end );
  1478.         }
  1479.         hitPt = newPt;
  1480.         GetMouse( &newPt );
  1481.         didScroll = eTeAutoScroll( hE, newPt );
  1482.     } while ( StillDown() );
  1483.  
  1484.     if ( start == end ) {
  1485.         /*
  1486.         (*pE).caretChPos = eTeOffsetToChPos( hE, start );
  1487.         (*pE).selActive = FALSE;
  1488.         (*pE).selStart = (*pE).caretChPos;
  1489.         (*pE).selEnd = (*pE).caretChPos;
  1490.         */
  1491.         (*pE).selActive = FALSE;
  1492.         (*pE).caretChPos = (*pE).selStart;
  1493.     }
  1494.     else {
  1495.         if( start == anchorStart )
  1496.             (*pE).caretChPos = (*pE).selEnd;
  1497.         else
  1498.             (*pE).caretChPos = (*pE).selStart;
  1499.     }
  1500.     eTeUpdateCaretRect( hE );
  1501.     (*pE).maxRight = (*pE).caretRect.right;
  1502.     eTeSetCaretState( hE, CARET_ON );
  1503.     HUnlock( (Handle )hE );
  1504. }
  1505.  
  1506. /* 2May92  e  -- Style */
  1507.  
  1508. static short eTeOffsetToRun( eRec **hE, long anOffset )
  1509. {
  1510.     register long a;
  1511.     register long *runPtr;
  1512.     register long qRuns, count, j;
  1513.  
  1514.     if ( anOffset < 0 )  return -1;    /* special case for j == 0 below */
  1515.     if ( anOffset == 0 )  return 0;    /* special case for empty text */
  1516.  
  1517.     runPtr = *(**hE).hRuns;
  1518.     count = 0;
  1519.     qRuns = (**hE).qRuns;
  1520.     while ( count < qRuns )    {
  1521.         j = (count + qRuns - 1) >> 1;
  1522.         if( anOffset < runPtr[ j ] )
  1523.             qRuns = j;
  1524.         else if( anOffset >= runPtr[ j + 1 ] )
  1525.             count = j + 1;
  1526.         else    {
  1527.             count = j;
  1528.             break;
  1529.         }
  1530.     }
  1531.     return count;
  1532. }
  1533.  
  1534. static short eTeRunToStyle( eRec **hE, short r )
  1535. {
  1536.     return ( ( r & 1 ) ^ (**hE).fustStyle );
  1537. }
  1538.  
  1539. /* 12Sep92  e  */
  1540. void eTeGetRun( eRec **hE, long *sta, long *end )
  1541. {
  1542.     long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  1543.     short run = eTeOffsetToRun( hE, car-1 );
  1544.     short sty = eTeRunToStyle( hE, run );
  1545.     if( run < 0 || sty == 0 )
  1546.       *sta = *end = car;
  1547.     else
  1548.     { long *runVec = *(**hE).hRuns;
  1549.       *sta = runVec[run];
  1550.       *end = runVec[run+1];
  1551.     }
  1552. }
  1553.  
  1554. static void eTeUpdateRuns( eRec **hE, long j, long k, long n, short ns )
  1555. {    /* removing chars j..k, inserting n chars of style ns */
  1556.     short jr = eTeOffsetToRun( hE, j - 1 );
  1557.     short kr = eTeOffsetToRun( hE, k );
  1558.     short js = eTeRunToStyle( hE, jr );
  1559.     short ks = eTeRunToStyle( hE, kr );
  1560.     long adj = n - ( k - j );
  1561.     long *runVec = *(**hE).hRuns;
  1562.     short qRuns = (**hE).qRuns;
  1563.     
  1564.     if( n == 0 ) ns = ks;
  1565.     
  1566.     if( ( ns != js ) || ( j == 0 ) )
  1567.     {    /* can't merge n with j */
  1568.         if( j == 0 ) (**hE).fustStyle = ns;
  1569.         /* 10Aug92  e  was: 
  1570.         if( (( n != 0 ) && ( kr == qRuns )) || (( ns != ks ) && ( kr <= jr + 1 )) ) */
  1571.         if( ( n != 0 ) && ( kr <= jr + 1 ) && ( kr == qRuns || ns != ks ) )
  1572.         {    /* grow */
  1573.             short indx, incr = ( ( j == 0 ) || ( kr == qRuns ) ) ? 1 : 2;
  1574.             qRuns += incr;
  1575.             if ( qRuns >= (**hE).runsAllocated )
  1576.             {    (**hE).runsAllocated += MIN_RUNS;
  1577.                 SetHandleSize( (Handle )(**hE).hRuns, (**hE).runsAllocated * sizeof( long ) );
  1578.                 runVec = *(**hE).hRuns;
  1579.             }
  1580.             kr = jr + incr;
  1581.             for( indx = qRuns; indx > kr; indx-- )
  1582.                 runVec[indx] = runVec[indx-incr] + adj;
  1583.             runVec[++jr] = j;
  1584.             if( jr != kr ) runVec[++jr] = j + n;
  1585.             (**hE).qRuns = qRuns;
  1586.             return;
  1587.         }
  1588.         runVec[++jr] = j;
  1589.     }
  1590. #ifdef use_old_buggy_code
  1591.     /* 16Sep92  e  was... */
  1592.     else if( ( kr == qRuns ) && ( ns == ks ) /* && ( ns == js ) */ )
  1593.         kr--;
  1594.     if( ns == ks )
  1595. #endif
  1596.     if( ns == ks && ( kr != qRuns || ns != js ) )    /* 16Sep92  e */
  1597.     {    /* merge k with n (1&3) */
  1598.         while( kr < qRuns ) runVec[++jr] = runVec[++kr] + adj;
  1599.     }
  1600.     else
  1601.     {    /* can't merge k with n (2&4) */
  1602.         if( k > runVec[kr] ) { runVec[++jr] = j + n; kr++; }    /*  13Oct92  e  */
  1603.         while( kr <= qRuns ) runVec[++jr] = runVec[kr++] + adj;
  1604.     }
  1605.     (**hE).qRuns = jr;
  1606.     /* see if we should shrink the runs vector */
  1607.     if ( jr + MIN_RUNS + MIN_RUNS < (**hE).runsAllocated )
  1608.     {    (**hE).runsAllocated = ( jr / MIN_RUNS + 1 ) * MIN_RUNS;
  1609.         SetHandleSize( (Handle )(**hE).hRuns, (**hE).runsAllocated * sizeof( long ) );
  1610.     }
  1611. }
  1612.  
  1613. /* ******** */
  1614.  
  1615. static short eTeUpdateLineStarts( eRec **hE, short firstLine )
  1616. {
  1617.     char hdlState;
  1618.     short error = noErr;
  1619.     register LineRec     *linePtr;
  1620.     register char        *charPtr, c;
  1621.     register short        numLines;
  1622.     register long        maxLine;    /* 28May92  e */
  1623.     register long        offsLine;    /* 28May92  e */
  1624.     register short        wrap;        /*  5Jul92  e */
  1625.     eRec    *pE;
  1626.     char    *pT;
  1627.  
  1628.     MoveHHi( (Handle )hE );
  1629.     HLock( (Handle )hE );
  1630.     pE = *hE;
  1631.     MoveHHi( (*pE).hText );
  1632.     HLock( (*pE).hText );
  1633.     pT  = *(*pE).hText;
  1634.     HLock( (Handle )(*pE).hLines );
  1635.  
  1636.     linePtr = *(*pE).hLines;
  1637.     numLines = firstLine + 1;
  1638.     
  1639.     maxLine = (firstLine == 0) ? 0 : (*pE).bounds.h;    /* 28May92  e  */
  1640.     wrap = (*pE).wrap;                                    /*  5Jul92  e  */
  1641.  
  1642.     /* Scan entire block of text, start with line firstLine */
  1643.     for ( charPtr = linePtr[ firstLine ] + pT; ( c = *charPtr ) != '\0';  ++charPtr )
  1644.     {    /* Found end of line. Store next line's starting position */
  1645.         if ( c == RETURN
  1646.              || ( --wrap <= 0 && (c = charPtr[1]) != RETURN && c != '\0' ) )
  1647.              /* 10Aug92  e  added RETURN test  |  11Aug92  e  added '\0' test  */
  1648.         {    wrap = (*pE).wrap;                                    /*  5Jul92  e  */
  1649.             offsLine = charPtr - pT + 1;
  1650.             if( ( offsLine - linePtr[firstLine] ) >= maxLine )    /* 10Aug92  e  was > */
  1651.                 maxLine = offsLine - linePtr[firstLine] + 1;    /* 10Aug92  e  +1 */
  1652.             linePtr[ ++firstLine ] = offsLine;
  1653.             /* */
  1654.             ++numLines;
  1655.             /* See if we need to allocate more space for our line starts */
  1656.             if ( numLines >= (*pE).linesAllocated )
  1657.             {    (*pE).linesAllocated += MIN_LINES;
  1658.                 HUnlock( (Handle )(*pE).hLines );
  1659.                 SetHandleSize( (Handle )(*pE).hLines, (*pE).linesAllocated * sizeof( LineRec ) );
  1660.                   error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  1661.                   if( error != noErr )
  1662.                   {    HUnlock( (*pE).hText );
  1663.                       HUnlock( (Handle )hE );
  1664.                       return error;
  1665.                   }
  1666.                 MoveHHi( (Handle )(*pE).hLines );
  1667.                 HLock( (Handle )(*pE).hLines );
  1668.                 linePtr = *(*pE).hLines;
  1669.             }
  1670.         }
  1671.     }
  1672.     /* Last entry contains length of entire text block */
  1673.     offsLine = charPtr - pT;
  1674.     if( ( offsLine - linePtr[firstLine] ) >= maxLine )            /* 10Aug92  e  was > */
  1675.         maxLine = offsLine - linePtr[firstLine] + 1;            /* 10Aug92  e  +1 */
  1676.     linePtr[ firstLine + 1 ] = offsLine;
  1677.  
  1678.     HUnlock( (*pE).hText );
  1679.     HUnlock( (Handle )(*pE).hLines );
  1680.  
  1681.     /* See if we should shrink our line starts block */
  1682.     if ( numLines + MIN_LINES < (*pE).linesAllocated ) {
  1683.         (*pE).linesAllocated = ( numLines / MIN_LINES + 1 ) * MIN_LINES;
  1684.         SetHandleSize( (Handle )(*pE).hLines, (*pE).linesAllocated * sizeof( LineRec ) );
  1685.           error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  1686.     }
  1687.  
  1688.     (*pE).bounds.h = Min( maxLine, 32767 );  /* 28May92  e  */
  1689.     (*pE).bounds.v = numLines;
  1690.     HUnlock( (Handle )hE );
  1691.     eTeAdjustScrollMax( hE );
  1692.     return error;
  1693. }
  1694.  
  1695. static short eTeSetTextGuts( eRec** hE, Handle hT, long numChars )
  1696. {    short error = noErr;
  1697.     if ( (**hE).hText )
  1698.         DisposHandle( (**hE).hText );
  1699.     if ( (*hT)[ numChars -1 ] != 0 ) {
  1700.         numChars += 1;
  1701.         SetHandleSize( hT, numChars );
  1702.           error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  1703.           if( error != noErr ) return error;
  1704.     }
  1705.     (*hT)[ numChars - 1 ] = '\0';
  1706.     (**hE).hText = hT;
  1707.     error = eTeUpdateLineStarts( hE, 0 );
  1708.       if( error != noErr ) return error;
  1709.     error = eTeNewRuns( hE, numChars - 1 );
  1710.       if( error != noErr ) return error;
  1711.     (**hE).caretChPos.h = (**hE).caretChPos.v = 0L;
  1712.     (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1713.     (**hE).maxRight = 0;
  1714.     eTeUpdateCaretRect( hE );
  1715.     (**hE).dirty = FALSE;
  1716.     eTeRefresh( hE );                            /* 17Dec92  e  */
  1717.     return error;
  1718. }
  1719.  
  1720.  
  1721. short eTeSetTextHandleDetabify( eRec **hE, Handle hT, short tabstops )
  1722. {    long count;
  1723.     long size = GetHandleSize( hT );
  1724.     short error = DeTabifyHandle( hT, &size, &count, tabstops >= 0 ? tabstops : (**hE).tabStops );
  1725.     if ( error != noErr ) return error;
  1726.     /* see if count lines fit in lineStarts array */
  1727.     if( count > ( 32767 - ( MIN_LINES >> 1) ) )
  1728.         return -357;
  1729.     if ( count >= (**hE).linesAllocated )
  1730.     {    (**hE).linesAllocated = count + ( MIN_LINES >> 1);
  1731.         SetHandleSize( (Handle )(**hE).hLines, (**hE).linesAllocated * sizeof( LineRec ) );
  1732.           error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  1733.           if( error != noErr )
  1734.           {    (**hE).linesAllocated = 0;
  1735.               return error;
  1736.           }
  1737.     }
  1738.     return eTeSetTextGuts( hE, hT, size );
  1739. }
  1740.  
  1741. short eTeSetTextHandle( eRec** hE, Handle hT )
  1742. {
  1743.     return eTeSetTextGuts( hE, hT, GetHandleSize( hT ) );
  1744. }
  1745.  
  1746. short eTeSetTextPtr( eRec** hE, Ptr pT, long numChars )
  1747. {
  1748.     Handle hT;
  1749.     
  1750.     if ( pT[ numChars -1 ] != 0 )
  1751.         numChars += 1;
  1752.     PtrToHand( pT, &hT, numChars );
  1753.     (*hT)[ numChars - 1 ] = '\0';
  1754.     return eTeSetTextGuts( hE, hT, numChars );
  1755. }
  1756.  
  1757. void eTeSetSelect( eRec **hE, long aStart, long anEnd )
  1758. {
  1759.     ChPos    first, last;
  1760.     /* Unhilite existing selection */
  1761.     if ( (**hE).selActive ) {
  1762.         first = (**hE).selStart;
  1763.         last = (**hE).selEnd;
  1764.         eTeHiliteRange( hE, first, last );
  1765.         (**hE).selActive = FALSE;
  1766.     }
  1767.     else
  1768.         eTeSetCaretState( hE, CARET_OFF );
  1769.  
  1770.     if ( aStart < 0 ) aStart = 0;
  1771.     if ( anEnd < 0 ) anEnd = 0;
  1772.     if ( aStart >= anEnd ) {
  1773.         (**hE).selStart = (**hE).selEnd = eTeOffsetToChPos( hE, aStart );
  1774.     }
  1775.     else {
  1776.         (**hE).selActive = TRUE;
  1777.         first = eTeOffsetToChPos( hE, aStart );
  1778.         last = eTeOffsetToChPos( hE, anEnd );
  1779.         (**hE).selStart = first;
  1780.         (**hE).selEnd = last;
  1781.         eTeHiliteRange( hE, first, last );
  1782.     }
  1783.     (**hE).caretChPos = (**hE).selEnd;
  1784.     eTeUpdateCaretRect( hE );
  1785.     (**hE).maxRight = (**hE).caretRect.right;
  1786. }
  1787.  
  1788. static Boolean eTeHasReturns( register char *data, register long len )
  1789. {
  1790.     while( len-- )
  1791.         if ( *data++ == RETURN )
  1792.             return TRUE;
  1793.     return FALSE;
  1794. }
  1795.  
  1796. /* the scrap  13Aug92  e  */
  1797.  
  1798. void     eTePutScrap()
  1799. {    OSErr        err;
  1800.  
  1801.     HLock( eTeScrap );
  1802.     /* Make a copy of the scrap and give to the clipboard */
  1803.     if( ( err = ZeroScrap() ) == noErr )
  1804.     {    err = PutScrap( eTeScrapLen, 'TEXT', *eTeScrap );
  1805.     }
  1806.     if( err == noErr )
  1807.     {    
  1808.     }
  1809.     else SysBeep( 3 );
  1810.     eTeScrapCnt = (InfoScrap())->scrapCount;
  1811.     HUnlock( eTeScrap );
  1812. }
  1813.  
  1814. static void puntUndoStuff()
  1815. {
  1816.     eTeUndoStuff.undoTeRec = NULL;
  1817.     eTeUndoStuff.undoTitle = utUndo;
  1818.     eTeUndoStuff.undoProc  = undoBugNi;
  1819. }
  1820.  
  1821. void     eTeGetScrap()
  1822. {    long offset, length;
  1823.  
  1824.     if( eTeScrapCnt != (InfoScrap())->scrapCount )
  1825.     {    length = GetScrap( NULL, 'TEXT', &offset );
  1826.         if ( length >= 0 )
  1827.         { eTeScrapLen = GetScrap( eTeScrap, 'TEXT', &offset );
  1828.           puntUndoStuff();
  1829.         }
  1830.         else if( length != noTypeErr )
  1831.         { SysBeep( 3 );
  1832.           SetHandleSize( eTeScrap, 0 );
  1833.           eTeScrapLen = 0;
  1834.           puntUndoStuff();
  1835.         }
  1836.         /* else punt: noTypeErr => nothing to copy */
  1837.     }
  1838. }
  1839.  
  1840. /* I don't feel bad using a large buffer on the stack,
  1841.    even on a MacPlus Quickdraw uses several K of stack space to draw text! */
  1842. #define TR_BUF_SZ 4000
  1843.  
  1844. /* ####################################################################
  1845.  
  1846. 14Aug92  e
  1847.  
  1848. Undo notes...
  1849.  
  1850. try to alloc a temp handle in dodoTyping()
  1851.  
  1852. concatenate successive kills (use clip or undo ?) ?
  1853.  
  1854. add to eTeKeyIns() mechanism for delete and del>
  1855.  
  1856.   if undoTyping or undoClear already active && there's no selection
  1857.  
  1858.     if normal char
  1859.       if caret >= undoStart && caret <= undoStart + insertLen
  1860.       increment insertLen
  1861.       insert char
  1862.     else
  1863.       start new undo & keyAccum
  1864.  
  1865.     if Delete
  1866.       if caret > undoStart && caret <= undoStart + insertLen
  1867.         decr insertLen
  1868.         do the delete
  1869.       else if caret == undoStart
  1870.         put character at caret-1 onto front of undoText
  1871.         do the delete
  1872.       else
  1873.         start new undo & keyAccum
  1874.  
  1875.     if Del>
  1876.       if caret >= undoStart && caret < undoStart + insertLen
  1877.         decr insertLen
  1878.         do the delete
  1879.       else if caret == undoStart + insertLen
  1880.         put character at caret onto end of undoText
  1881.         do the delete
  1882.       else
  1883.         start new undo & keyAccum
  1884.  
  1885.   else
  1886.     what's done now
  1887.  
  1888. ensureSelOK() is only necessary
  1889.  because caretChPos is not kept syncronized with selStart & selEnd and vice versa
  1890.  should this be fixed so ensureSelOK() can be eliminated?  for other reasons?
  1891.  
  1892. make eTeTranspose() work better with undo -- at least adjust offsets?
  1893.  
  1894. subroutinize undo routines
  1895.   -- eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 ); ??
  1896.   -- hoseUndoText();
  1897.   -- rsSelectionPts();
  1898.  
  1899. /* ##################### */
  1900.  
  1901. static void ensureSelOK( eRec **hE )                /* can this be eliminated !? */
  1902. {    if( ! (**hE).selActive )
  1903.         (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1904. }
  1905.  
  1906. void eTeUndo( eRec **hE )
  1907. {
  1908.     if( hE == eTeUndoStuff.undoTeRec )
  1909.         eTeUndoStuff.undoProc( hE );
  1910.     else SysBeep( 3 );
  1911. }
  1912.  
  1913. void eTeEditMenuUpdate( eRec **hE, MenuHandle hM )
  1914. {
  1915.     SetItem( hM, 1, eTeUndoStuff.undoTitle );        /* memoize this ?! */
  1916.     if( hE != NULL )
  1917.     {    if( (**hE).selActive )
  1918.         {    EnableItem( hM, 3 ); /* cut */
  1919.             EnableItem( hM, 4 ); /* copy */
  1920.         }
  1921.         else
  1922.         {    DisableItem( hM, 3 ); /* cut */
  1923.             DisableItem( hM, 4 ); /* copy */
  1924.         }
  1925.         EnableItem( hM, 5 ); /* paste */
  1926.         EnableItem( hM, 6 ); /* clear */
  1927.         if( hE == eTeUndoStuff.undoTeRec )
  1928.             EnableItem( hM, 1 );
  1929.         else
  1930.             DisableItem( hM, 1 ); /* undo */
  1931.     }
  1932.     else
  1933.     {    EnableItem( hM, 3 ); /* cut */
  1934.         EnableItem( hM, 4 ); /* copy */
  1935.         EnableItem( hM, 5 ); /* paste */
  1936.         EnableItem( hM, 6 ); /* clear */
  1937.         DisableItem( hM, 1 ); /* undo */
  1938.     }
  1939. }
  1940.  
  1941. static void ssSelectionPts( eRec **hE )
  1942. {    ensureSelOK( hE );
  1943.     eTeUndoStuff.undoInLen = 0;
  1944.     eTeUndoStuff.undoKeyAccum = FALSE;
  1945.     eTeUndoStuff.undoStart = eTeChPosToOffset( hE, (**hE).selStart );
  1946.     eTeUndoStuff.undoEnd   = eTeChPosToOffset( hE, (**hE).selEnd   );
  1947.     eTeUndoStuff.undoCarStaP = PTL( (**hE).caretChPos ) == PTL( (**hE).selStart );
  1948.     eTeUndoStuff.undoDirty = (**hE).dirty;
  1949.     eTeUndoStuff.undoTeRec = hE;
  1950. }
  1951.  
  1952. static void ssSwapScrap()
  1953. {    Handle h;
  1954.     long l;
  1955.     h = eTeScrap;
  1956.     l = eTeScrapLen;
  1957.     eTeScrap = eTeUndoStuff.undoText;
  1958.     eTeScrapLen = eTeUndoStuff.undoTxLen;
  1959.     eTeUndoStuff.undoText = h;
  1960.     eTeUndoStuff.undoTxLen = l;
  1961. }
  1962.  
  1963. static void eTeSetSelCar( eRec **hE, long aStart, long anEnd, Boolean stap )
  1964. {
  1965.     eTeSetSelect( hE, aStart, anEnd );
  1966.     if( stap ) (**hE).caretChPos = (**hE).selStart;
  1967. }
  1968.  
  1969. static void eTePasteNu( eRec **hE, short style )
  1970. {
  1971.     MoveHHi( eTeScrap );
  1972.     HLock( eTeScrap );
  1973.     eTeEdGuts( hE, *eTeScrap, eTeScrapLen, eTeHasReturns( *eTeScrap, eTeScrapLen ), TRUE, style );
  1974.     HUnlock( eTeScrap );
  1975. }
  1976.  
  1977. static void eTeCopyNu( eRec **hE )
  1978. {
  1979.     long        length, offset;
  1980.  
  1981.     /* Get starting position and length of text to copy */
  1982.     offset = eTeChPosToOffset( hE, (**hE).selStart );
  1983.     length = eTeChPosToOffset( hE, (**hE).selEnd ) - offset;
  1984.     MoveHHi( (**hE).hText );
  1985.     HLock( (**hE).hText );
  1986.     /* Make a copy of the text and put in scrap */
  1987.     offset = Munger( eTeScrap, 0L, NULL, eTeScrapLen, *(**hE).hText + offset, length );
  1988.     if( offset == length )
  1989.         eTeScrapLen = length; /* success */
  1990.     else SysBeep( 3 );
  1991.     HUnlock( (**hE).hText );
  1992. }
  1993.  
  1994. static void undoCut( eRec  **hE )                /* Undo Cut */
  1995. {                                                /* restore selection from scrap */
  1996.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  1997.     eTePasteNu( hE, 0 );
  1998.     ssSwapScrap();                                 /* restore scrap */
  1999.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2000.     eTeUndoStuff.undoTxLen = 0;
  2001.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2002.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2003.     eTeUndoStuff.undoTitle = rtCut;
  2004.     eTeUndoStuff.undoProc  = eTeCut;
  2005. }
  2006.   
  2007. void eTeCut( eRec **hE )                        /*  Cut == RedoCut  */
  2008. {    if( ! (**hE).selActive ) return;
  2009.     ssSelectionPts( hE );  /* snapshot selection points */
  2010.     ssSwapScrap();         /* snapshot scrap */
  2011.     eTeUndoStuff.undoTitle = utCut;
  2012.     eTeUndoStuff.undoProc  = undoCut;
  2013.     eTeCopyNu( hE );                            /* do cut */
  2014.     eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );
  2015. }
  2016.  
  2017. static void undoCopy( eRec  **hE )                /* Undo Copy */
  2018. {    
  2019.     ssSwapScrap();                                 /* restore scrap */
  2020.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2021.     eTeUndoStuff.undoTxLen = 0;
  2022.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2023.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2024.     eTeUndoStuff.undoTitle = rtCopy;
  2025.     eTeUndoStuff.undoProc  = eTeCopy;
  2026. }
  2027.   
  2028. void eTeCopy( eRec **hE )                        /* Copy == Redo Copy */
  2029. {    if( ! (**hE).selActive ) return;
  2030.     ssSelectionPts( hE );                          /* snapshot selection points */
  2031.     ssSwapScrap();                                 /* snapshot scrap */
  2032.     eTeUndoStuff.undoTitle = utCopy;
  2033.     eTeUndoStuff.undoProc  = undoCopy;
  2034.     eTeCopyNu( hE );                            /* do copy */
  2035. }
  2036.  
  2037. static void redoPaste( eRec  **hE )                /* Redo Paste */
  2038. {    eTePaste( hE, 0 );
  2039. }
  2040.  
  2041. static void undoPaste( eRec  **hE )                /* Undo Paste */
  2042. {                                                /* restore selection from scrap */
  2043.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart + eTeScrapLen );
  2044.     ssSwapScrap();                                 /* swap scrap & undo */
  2045.     eTePasteNu( hE, 0 );                        /* restore text from undo */
  2046.     ssSwapScrap();                                 /* swap scrap & undo */
  2047.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2048.     eTeUndoStuff.undoTxLen = 0;
  2049.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2050.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2051.     eTeUndoStuff.undoTitle = rtPaste;
  2052.     eTeUndoStuff.undoProc  = redoPaste;
  2053. }
  2054.  
  2055. void eTePaste( eRec **hE, short style )            /* Paste != Redo Paste */
  2056. {    ssSelectionPts( hE );                          /* snapshot selection points */
  2057.     ssSwapScrap();                                 /* snapshot selection */
  2058.     eTeCopyNu( hE );
  2059.     ssSwapScrap();                                 /* restore scrap */
  2060.     eTeUndoStuff.undoTitle = utPaste;
  2061.     eTeUndoStuff.undoProc  = undoPaste;
  2062.     eTePasteNu( hE, style );                    /* do paste [14Sep92  e  -- added style back] */
  2063. }
  2064.  
  2065. static void undoDelete( eRec **hE )                /* Undo Clear */
  2066. {                                                /* restore selection from scrap */
  2067.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2068.     ssSwapScrap();                                 /* swap scrap & undo */
  2069.     eTePasteNu( hE, 0 );                        /* restore text from undo */
  2070.     ssSwapScrap();                                 /* swap scrap & undo */
  2071.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2072.     eTeUndoStuff.undoTxLen = 0;
  2073.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2074.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2075.     eTeUndoStuff.undoTitle = rtClear;
  2076.     eTeUndoStuff.undoProc  = eTeDelete;
  2077. }
  2078.  
  2079. void eTeDelete( eRec **hE )                        /* Clear == Redo Clear */
  2080. {    long offset;
  2081.     if ( ! (**hE).selActive )
  2082.     {    /* Just delete previous character */
  2083.         offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  2084.         if ( offset <= 0 ) return;
  2085.         ssSelectionPts( hE );                          /* snapshot selection points */
  2086.         eTeUndoStuff.undoStart--;
  2087.         (**hE).selEnd = (**hE).caretChPos;
  2088.         (**hE).selStart = eTeOffsetToChPos( hE, --offset );
  2089.     }
  2090.     else
  2091.         ssSelectionPts( hE );                          /* snapshot selection points */
  2092.     ssSwapScrap();                                 /* snapshot selection */
  2093.     eTeCopyNu( hE );
  2094.     ssSwapScrap();                                 /* restore scrap */
  2095.     eTeUndoStuff.undoTitle = utClear;
  2096.     eTeUndoStuff.undoProc  = undoDelete;
  2097.     if ( (**hE).selActive )
  2098.         eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );
  2099.     else
  2100.     {    (**hE).selEnd = (**hE).caretChPos = (**hE).selStart;
  2101.         eTeSetCaretState( hE, CARET_OFF ); /* 23Jul92  e */
  2102.         eTeUpdateCaretRect( hE );
  2103.         eTeEdGuts( hE, EMPTY_PTR, -1L, *(*(**hE).hText + offset) == RETURN, TRUE, 0 );
  2104.     }
  2105. }
  2106.  
  2107. static void redoKill( eRec **hE )                /* Redo Kill */
  2108. {
  2109.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2110.     eTeKillTo( hE, eTeOffsetToChPos( hE, eTeUndoStuff.undoEnd ) );
  2111. }
  2112.  
  2113. static void undoKill( eRec  **hE )                /* Undo Kill */
  2114. {                                                /* restore selection from scrap */
  2115.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2116. #if Kill_Deletes
  2117.     ssSwapScrap();                                 /* swap scrap & undo */
  2118. #endif
  2119.     eTePasteNu( hE, 0 );
  2120.     ssSwapScrap();                                 /* restore scrap */
  2121.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2122.     eTeUndoStuff.undoTxLen = 0;
  2123.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2124.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2125.     eTeUndoStuff.undoTitle = rtKill;
  2126.     eTeUndoStuff.undoProc  = redoKill;
  2127. }
  2128.   
  2129. void eTeKillTo( eRec **hE, ChPos chPos )        /* Kill != Redo Kill */
  2130. {
  2131.     if( PTL( chPos ) == PTL( (**hE).caretChPos ) ) return;
  2132.     (**hE).selStart = (**hE).caretChPos;        /* mung selection points first! */
  2133.     (**hE).selEnd = chPos;
  2134.     (**hE).selActive = TRUE;
  2135.     ssSelectionPts( hE );                          /* snapshot selection points */
  2136.     ssSwapScrap();                                 /* snapshot scrap */
  2137.     eTeCopyNu( hE );
  2138. #if Kill_Deletes
  2139.     ssSwapScrap();                                 /* restore scrap */
  2140. #endif
  2141.     eTeUndoStuff.undoTitle = utKill;
  2142.     eTeUndoStuff.undoProc  = undoKill;
  2143.     /* since we'll redraw the line anyway, eTeHiliteRange isn't necessary!?    */
  2144.     /*  unfortunately, for wrapped lines, it is - otherwise cursor turds; ugh. */
  2145.     /* eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );                   */
  2146.     /* instead we'll fake it out by making the window look inactive instead    */
  2147.     (**hE).active = FALSE;
  2148.     eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );
  2149.     (**hE).active = TRUE;
  2150. }
  2151.  
  2152. static Boolean dodoTyping( eRec **hE )
  2153. {    char buf[TR_BUF_SZ];
  2154.     long sz;
  2155.     register char *p, *q;
  2156.     register long iter = sz = eTeUndoStuff.undoTxLen;;
  2157.     
  2158.     if( iter > 0 || eTeUndoStuff.undoInLen > 0 )
  2159.     {
  2160.         if( eTeUndoStuff.undoInLen > TR_BUF_SZ )
  2161.         {    SysBeep( 6 );                            /* try to alloc a temp handle ? */
  2162.             return FALSE;
  2163.         }
  2164.         q = buf;
  2165.         p = *eTeUndoStuff.undoText;
  2166.         while( iter-- ) *q++ = *p++;
  2167.         eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart + eTeUndoStuff.undoInLen );
  2168.         ssSwapScrap();                                 /* snapshot insertion */
  2169.         eTeCopyNu( hE );
  2170.         ssSwapScrap();                                 /* restore scrap */
  2171.         eTeEdGuts( hE, buf, sz, eTeHasReturns( buf, sz ), TRUE, 0 );
  2172.         eTeUndoStuff.undoInLen = sz;
  2173.     }
  2174.     return TRUE;
  2175. }
  2176.  
  2177. static void redoTyping( eRec **hE );
  2178.  
  2179. static void undoTyping( eRec **hE )
  2180. {    if( dodoTyping( hE ) )
  2181.     {    eTeUndoStuff.undoKeyAccum = FALSE;
  2182.         eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2183.         (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2184.         eTeUndoStuff.undoTitle = rtTyping;
  2185.         eTeUndoStuff.undoProc  = redoTyping;
  2186.     }
  2187. }
  2188.     
  2189. static void redoTyping( eRec **hE )
  2190. {    eTeUndoStuff.undoKeyAccum = TRUE;
  2191.     eTeUndoStuff.undoDirty = (**hE).dirty;
  2192.     if( dodoTyping( hE ) )
  2193.     {    eTeUndoStuff.undoTitle = utTyping;
  2194.         eTeUndoStuff.undoProc  = undoTyping;
  2195.     }
  2196.     else
  2197.     {    eTeUndoStuff.undoKeyAccum = FALSE;
  2198.     }
  2199. }
  2200.  
  2201. static void eTeKeyIns( eRec **hE, char c, short style )
  2202. {
  2203.     if( eTeUndoStuff.undoKeyAccum
  2204.         && hE == eTeUndoStuff.undoTeRec
  2205.         && (**hE).selActive == FALSE
  2206.         && eTeChPosToOffset( hE, (**hE).caretChPos )
  2207.              == eTeUndoStuff.undoStart + eTeUndoStuff.undoInLen
  2208.       )
  2209.     {    eTeUndoStuff.undoInLen += 1;            /* incr numChars inserted */
  2210.     }
  2211.     else
  2212.     {    ssSelectionPts( hE );                      /* snapshot selection points */
  2213.         eTeUndoStuff.undoKeyAccum = TRUE;
  2214.         ssSwapScrap();                             /* snapshot selection */
  2215.         eTeCopyNu( hE );
  2216.         ssSwapScrap();                             /* restore scrap */
  2217.         eTeUndoStuff.undoInLen = 1;                /* remember numChars inserted */
  2218.         eTeUndoStuff.undoTitle = utTyping;
  2219.         eTeUndoStuff.undoProc  = undoTyping;
  2220.     }
  2221.     eTeEdGuts( hE, &c, 1, c == RETURN, TRUE, style );
  2222. }
  2223.  
  2224. void eTeInsert( eRec **hE, Ptr textPtr, long numChars, short style )
  2225. {    if( numChars <= 0 ) { eTeDelete( hE ); return; } /* 08Jan93  e  */
  2226.     ssSelectionPts( hE );                          /* snapshot selection points */
  2227.     eTeUndoStuff.undoKeyAccum = TRUE;
  2228.     ssSwapScrap();                                 /* snapshot selection */
  2229.     eTeCopyNu( hE );
  2230.     ssSwapScrap();                                 /* restore scrap */
  2231.     eTeUndoStuff.undoInLen = numChars;            /* remember numChars inserted */
  2232.     eTeUndoStuff.undoTitle = utInsert;
  2233.     eTeUndoStuff.undoProc  = undoTyping;
  2234.     eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), TRUE, style );
  2235. }
  2236.  
  2237. /* #################################################################### */
  2238.  
  2239. /* eTeTranspose  22Jul92  e  */
  2240.  
  2241. static void eTeTransposeGuts1( eRec **hE, long beg, long mid, long end )
  2242. { char buf[TR_BUF_SZ];
  2243.   long shift = mid - beg;
  2244.   register char *p, *q;
  2245.   register long incr, iter;
  2246.   
  2247.   while( shift > 0 )
  2248.   { incr = Min( shift, TR_BUF_SZ );
  2249.     shift -= incr;
  2250.     /* */
  2251.     p = *(**hE).hText + beg;
  2252.     q = buf;
  2253.     iter = incr;
  2254.     while( iter-- ) *q++ = *p++;
  2255.     /* */
  2256.     p = *(**hE).hText + beg + incr;
  2257.     q = *(**hE).hText + beg;
  2258.     iter = end - beg - incr;
  2259.     while( iter-- ) *q++ = *p++;
  2260.     /* */
  2261.     p = buf;
  2262.     q = *(**hE).hText + end - incr;
  2263.     iter = incr;
  2264.     while( iter-- ) *q++ = *p++;
  2265.   }
  2266. }
  2267.  
  2268. static void eTeTransposeGuts0( eRec **hE, long beg, long midl, long midr, long end )
  2269. { char buf[TR_BUF_SZ];
  2270.   register char *p, *q;
  2271.   register long iter;
  2272.   
  2273.   q = buf;
  2274.   p = *(**hE).hText + midl;
  2275.   iter = midr - midl;
  2276.   while( iter-- ) *q++ = *p++;
  2277.   p = *(**hE).hText + beg;
  2278.   iter = midl - beg;
  2279.   while( iter-- ) *q++ = *p++;
  2280.   q = *(**hE).hText + beg;
  2281.   p = *(**hE).hText + midr;
  2282.   iter = end - midr;
  2283.   while( iter-- ) *q++ = *p++;
  2284.   p = buf;
  2285.   iter = midr - beg;
  2286.   while( iter-- ) *q++ = *p++;
  2287. }
  2288.  
  2289. static void eTeTransposeGuts2( eRec **hE, long beg, long midl, long midr, long end )
  2290.   if( midr - beg <= TR_BUF_SZ )
  2291.   { eTeTransposeGuts0( hE, beg, midl, midr, end );
  2292.   }
  2293.   else
  2294.   { eTeTransposeGuts1( hE, beg, midl, midr );
  2295.     eTeTransposeGuts1( hE, beg, midr, end );
  2296.   }
  2297. }
  2298.  
  2299. void eTeTranspose( eRec **hE, long beg, long midl, long midr, long end )
  2300. { long offs = beg + end - midr;
  2301.   long numChars = end - beg;
  2302.   char *textPtr;
  2303.   
  2304.   if ( (**hE).selActive
  2305.         || beg  <  0
  2306.         || beg  >= midl
  2307.         || midl >  midr
  2308.         || midr >= end
  2309.         || end  >  eTeTextLength( hE ) )
  2310.  { SysBeep(3);
  2311.    return;
  2312.   }
  2313.   eTeSetCaretState( hE, CARET_OFF );
  2314.   if( midl == midr )
  2315.     eTeTransposeGuts1( hE, beg, midr, end );
  2316.   else
  2317.     eTeTransposeGuts2( hE, beg, midl, midr, end );
  2318.   (**hE).selStart = eTeOffsetToChPos( hE, beg );
  2319.   (**hE).selEnd   = eTeOffsetToChPos( hE, end );
  2320.   (**hE).selActive = TRUE;
  2321.   textPtr = *(**hE).hText + beg;
  2322.   /* fake it out by making the window look inactive */
  2323.   (**hE).active = FALSE;
  2324.   eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), FALSE, 0 );
  2325.   (**hE).active = TRUE;
  2326.   eTeSetSelect( hE, offs, offs );
  2327.   eTeShowCaret( hE );
  2328.   puntUndoStuff();                 /* all bets off for now    --  17Aug92  e */
  2329. }
  2330.  
  2331. /* eTeWrite  9Jul92  e  */
  2332.  
  2333. void eTeWrite( eRec **hE, Ptr textPtr, long numChars, short style )
  2334. {    /* save selection, etc. */
  2335.     long sta = eTeChPosToOffset( hE, (**hE).selStart );
  2336.     long end = eTeChPosToOffset( hE, (**hE).selEnd );
  2337.     long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  2338.     long pos = eTeChPosToOffset( hE, (**hE).writeChPos );
  2339.     short mxr = (**hE).maxRight;                            /* 10Aug92  e  */
  2340.     Boolean mxrp = mxr != (**hE).caretRect.right;            /* 10Aug92  e  */
  2341.     Boolean selp = (**hE).selActive;
  2342.     Boolean stap = selp && car == sta;
  2343.     if( selp ) eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  2344.     else       eTeSetCaretState( hE, CARET_OFF );           /*  14Jul92  e  */
  2345.     /* swap caretChPos & writeChPos */
  2346.     (**hE).selActive = FALSE;
  2347.     (**hE).caretChPos = (**hE).writeChPos;
  2348.     eTeUpdateCaretRect( hE );
  2349.     /* kludge for bs -- 17Mar94  e  */
  2350.     if ( numChars == 1 && *textPtr == '\b' && pos > 0 )
  2351.     { numChars = -1;
  2352.       (**hE).caretChPos = eTeOffsetToChPos( hE, --pos );
  2353.       eTeUpdateCaretRect( hE );
  2354.       eTeEdGuts( hE, EMPTY_PTR, numChars, *(*(**hE).hText + pos) == RETURN, ( pos == car ), style );
  2355.     }
  2356.     else /* end of insert                                   --  17Mar94  e  */
  2357.     /* show was always FALSE; changed to ( pos == car )     --  14Jul92  e  */
  2358.     eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), ( pos == car ), style );
  2359.     /* set writeChPos to caretChPos */
  2360.     (**hE).writeChPos = (**hE).caretChPos;
  2361.     /* restore selection, etc. */
  2362.     if( selp )
  2363.     { if( pos < end )
  2364.       { end += numChars;
  2365.         if( pos <= sta ) sta += numChars;
  2366.       }
  2367.     }
  2368.     else
  2369.     { if( pos <= car ) car += numChars;
  2370.       sta = end = car;
  2371.     }
  2372.     eTeSetSelCar( hE, sta, end, stap );
  2373.     if( mxrp ) (**hE).maxRight = mxr;                        /* 10Aug92  e  */
  2374.     if( hE == eTeUndoStuff.undoTeRec )                        /* 17Aug92  e  */    
  2375.     {    if( pos <= eTeUndoStuff.undoStart )
  2376.         {    eTeUndoStuff.undoEnd   += numChars;
  2377.             eTeUndoStuff.undoStart += numChars;
  2378.         }
  2379.         else if( pos < eTeUndoStuff.undoEnd )
  2380.             puntUndoStuff(); /* all bets off */
  2381.         /* else OK, a write after undo stuff */
  2382.     }
  2383. }
  2384.  
  2385. /* 11Aug92  e   rewritten with eol and sol and new eTeUpdateLineStarts case  */
  2386.  
  2387. static Boolean eTeAdjustLineStarts( eRec **hE, register short start, short amount )
  2388. {
  2389.     register short bot = (**hE).bounds.v;
  2390.     register LineRec *linePtr;
  2391.     register long incr;
  2392.     long llen;
  2393.     long sol, eol;
  2394.     char *pT, c;
  2395.  
  2396.     if ( ( incr = (long )amount ) != 0 ) {
  2397.         /* 28May92  e  was: linePtr = *(**hE).hLines + start + 1; */
  2398.         pT = *(**hE).hText;
  2399.         linePtr = *(**hE).hLines + start;
  2400.         sol = *linePtr++;
  2401.         eol = *linePtr + incr;
  2402.         llen = eol - sol;
  2403.         if( llen > (**hE).bounds.h )
  2404.         { (**hE).bounds.h = Min( llen, ( (**hE).wrap + 1 ) );
  2405.           HLock( (Handle )(**hE).hLines );
  2406.           eTeAdjustScrollMax( hE );
  2407.           HUnlock( (Handle )(**hE).hLines );
  2408.         }
  2409. #ifdef use_old_code
  2410.         /* 12Aug92  e  moved to eTeEdGuts() since it's also applicable when hasReturn is TRUE */
  2411.         if( start > 0
  2412.             && pT[sol-1] != RETURN
  2413.             && ( (c = pT[sol]) == RETURN || c == '\0' ) )
  2414.         { /* this line starts off with a RETURN or NUL and prev line has no RETURN */
  2415.           eTeUpdateLineStarts( hE, start - 1 );
  2416.           return TRUE; /* -1 ? */
  2417.         }
  2418.         else
  2419. #endif
  2420.         if( llen > (**hE).wrap || ( (c = pT[eol-1]) != RETURN && c != '\0' ) )
  2421.         { /* this line is longer than wrap or already doesn't end with a RETURN or NUL */
  2422.           eTeUpdateLineStarts( hE, start );
  2423.           return TRUE; /* +1 ? */
  2424.         }
  2425.         while ( start++ < bot )
  2426.             *linePtr++ += incr;
  2427.     }
  2428.     return FALSE;
  2429. }
  2430.  
  2431. /* eTeEdGuts
  2432.     handles insertion, deletion, and substitution
  2433.     If selection is active, then the text of the selection is deleted, then...
  2434.     If numChars is greater than 0, then the given text is inserted;
  2435.      else if numChars is equal to 0, then no text is inserted;
  2436.      otherwise (numChars is less than 0) AND THIS ONLY WORKS IF NO SELECTION...
  2437.        minus numChars characters to the RIGHT of the caret position are deleted.
  2438. */
  2439.  
  2440. static void eTeEdGuts( eRec **hE, Ptr textPtr, long numChars,
  2441.                              Boolean hasReturn, Boolean show, short style )
  2442. {
  2443.     register long    oldLen, offset;
  2444.     Rect            invalidRect;
  2445.     long newWrite = -1; /* 11Aug92  e   was: 0 */
  2446.     char *pT;            /* 12Aug92  e  */
  2447.  
  2448.     if ( (**hE).selActive ) {
  2449.         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  2450.         (**hE).selActive = FALSE;
  2451.         offset = eTeChPosToOffset( hE, (**hE).selStart );
  2452.         oldLen = eTeChPosToOffset( hE, (**hE).selEnd ) - offset;
  2453.         if( numChars < 0 ) numChars = 0;    /*  17Aug92  e   */
  2454.         if( (**hE).selEnd.v != (**hE).selStart.v )
  2455.             hasReturn = TRUE; /* 18May92  e   Oops! */
  2456.         else                  /*  5Jul92  2   Oops! */
  2457.             hasReturn |= eTeHasReturns( *(**hE).hText + offset, oldLen );
  2458.         /* 9Jul92  e */
  2459.         if( PTL( (**hE).writeChPos ) > PTL( (**hE).selStart ) )
  2460.         { if( PTL( (**hE).writeChPos ) < PTL( (**hE).selEnd ) )
  2461.             (**hE).writeChPos = (**hE).selStart;
  2462.           else
  2463.             newWrite = eTeChPosToOffset( hE, (**hE).writeChPos ) + numChars - oldLen;
  2464.         }
  2465.         /* */
  2466.         (**hE).caretChPos = (**hE).selEnd = (**hE).selStart;
  2467.         /* 3May92  e  broken by AppleArrows
  2468.         topLeft( invalidRect ) = eTeChPosToPoint( hE, (**hE).caretChPos );
  2469.         invalidRect.left -= 1;
  2470.         */
  2471.         eTeUpdateCaretRect( hE );
  2472.         topLeft( invalidRect ) = topLeft( (**hE).caretRect );
  2473.     }
  2474.     else {
  2475.         eTeSetCaretState( hE, CARET_OFF ); /* 23Jul92  e */
  2476.         offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  2477.         /*  17Aug92  e  was: oldLen = numChars ? 0 : 1; */
  2478.         if( numChars < 0 )
  2479.         {    oldLen = - numChars;
  2480.             numChars = 0;
  2481.         }
  2482.         else oldLen = 0;
  2483.         /* 9Jul92  e */
  2484.         if( PTL( (**hE).writeChPos ) > PTL( (**hE).caretChPos ) )
  2485.           newWrite = eTeChPosToOffset( hE, (**hE).writeChPos ) + numChars - oldLen;
  2486.         /* */
  2487.         topLeft( invalidRect ) = topLeft( (**hE).caretRect );
  2488.     }
  2489.  
  2490.     /* Do substitution/insertion */
  2491.     Munger( (**hE).hText, offset, NULL, oldLen, textPtr, numChars );
  2492.     (**hE).dirty = TRUE;
  2493.     
  2494.     /* 12Aug92  e    as a result of adding line wrap, need to check if
  2495.                      preceeding line could be 'unwrapped' by this edit...
  2496.     */
  2497.     if( (**hE).caretChPos.h == 0                          /* edit is at the start of a line */
  2498.         && offset > 0                                      /* and it's not the first line */
  2499.         && ((pT=*(**hE).hText))[offset-1] != RETURN       /* and the prev line is a wrapped line */
  2500.         && ( pT[offset] == RETURN || pT[offset] == '\0' ) /* and this line starts with CR or NUL */
  2501.       )
  2502.     {    hasReturn = TRUE;
  2503.         eTeUpdateLineStarts( hE, (**hE).caretChPos.v - 1 );
  2504.         (**hE).caretChPos = eTeOffsetToChPos( hE, offset );
  2505.         eTeUpdateCaretRect( hE );
  2506.         topLeft( invalidRect ) = topLeft( (**hE).caretRect );
  2507.     }
  2508.     else if( hasReturn )
  2509.         eTeUpdateLineStarts( hE, (**hE).caretChPos.v );
  2510.     else
  2511.         hasReturn |= eTeAdjustLineStarts( hE, (**hE).caretChPos.v, numChars - oldLen );
  2512.     
  2513.     eTeUpdateRuns( hE, offset, offset + oldLen, numChars, style );
  2514.  
  2515.     invalidRect.right = (**hE).viewRect.right;        /* 21Jul92  e  moved outside of cond */
  2516.     
  2517.     /* Redraw the current line, starting at the caret */
  2518.     if ( invalidRect.left < (**hE).viewRect.right ) {
  2519.         invalidRect.bottom = invalidRect.top + (**hE).vScale;
  2520.         eTePrepare( hE );
  2521.         ++invalidRect.left;  /* move up here 23Jul92  e  */
  2522.         EraseRect( &invalidRect );
  2523.         /* ++invalidRect.left; !?  22Jul92  e  */
  2524.         eTeDrawLine( hE, (**hE).caretChPos, topLeft( invalidRect ) );
  2525.     }
  2526.  
  2527.     /* if RETURN added, redraw from the next line to the end of the screen */
  2528.     if ( hasReturn ) {
  2529.         invalidRect.top += (**hE).vScale;
  2530.         /* this left ghost on left edge when xor caret used...
  2531.         invalidRect.left = (**hE).leftMargin - (**hE).hOrigin;
  2532.         23Jul92  e  -- instead... */
  2533.         invalidRect.left =  (**hE).viewRect.left;
  2534.         invalidRect.bottom = (**hE).viewRect.bottom;
  2535.         /* eTePrepare( hE );  -- done by eTeDraw  21Jul92  e  */
  2536.         eTeDraw( hE, &invalidRect );
  2537.         (**hE).caretChPos = eTeOffsetToChPos( hE, offset + numChars );
  2538.         eTeUpdateCaretRect( hE );
  2539.     }
  2540.     /* Otherwise, just adjust the caret point by moving it over the inserted text */
  2541.     else {
  2542.          eTePrepare( hE );
  2543.          eTePrepareStyle( hE, style );
  2544.          (**hE).caretChPos.h += numChars;
  2545.         for ( ; numChars > 0 ; ++textPtr, --numChars ) {
  2546.             if ( *textPtr == TAB ) {
  2547.                 (**hE).caretRect.right = eTeTabStop( hE, (**hE).caretRect.right );
  2548.             }
  2549.             else
  2550.                 (**hE).caretRect.right += CharWidth( *textPtr );
  2551.         }
  2552.         (**hE).caretRect.left = (**hE).caretRect.right - 1;
  2553.     }
  2554.     /* 9Jul92  e */
  2555.     if( newWrite >= 0 ) /* 11Aug92  e   was: != 0 */
  2556.       (**hE).writeChPos = eTeOffsetToChPos( hE, newWrite );
  2557.     /* */
  2558.     if( show ) eTeShowCaret( hE );
  2559.     (**hE).maxRight = (**hE).caretRect.right;    /*  02Oct92  e  */
  2560. }
  2561.  
  2562. static short eTeChPosVToPointV( eRec **hE, short v )
  2563. {
  2564.     register long tmp;
  2565.  
  2566.     tmp = (long )v * (**hE).vScale + (**hE).topMargin - (**hE).vOrigin;
  2567.     if( tmp < -32000 )
  2568.         return -32000;
  2569.     if( tmp > 32000 )
  2570.         return 32000;
  2571.     return (short )tmp;
  2572. }
  2573.  
  2574. Point eTeChPosToPoint( eRec **hE, register ChPos aPos )
  2575. {
  2576.     register char *charPtr, c;
  2577.     register short left;
  2578.     long     offset, instyle;
  2579.     short     run;
  2580.     Point    pt;
  2581.  
  2582.     SignedByte hdlState;
  2583.  
  2584.     hdlState = HGetState( (**hE).hText );
  2585.     HLock( (**hE).hText );
  2586.  
  2587.     eTePrepare( hE );
  2588.     offset = *( *(**hE).hLines + aPos.v );
  2589.     charPtr = *(**hE).hText + offset;
  2590.     run = eTeOffsetToRun( hE, offset );
  2591.     left = (**hE).leftMargin - (**hE).hOrigin;
  2592.     do
  2593.     {    eTePrepareRun( hE, run );
  2594.         instyle = (*(**hE).hRuns)[++run] - offset;
  2595.         offset += instyle;
  2596.         while ( instyle-- && aPos.h-- )
  2597.         {    c = *charPtr++;
  2598.             if ( c == TAB )
  2599.                 left = eTeTabStop( hE, left );
  2600.             else
  2601.                 left += CharWidth( c );
  2602.         }
  2603.     } while( aPos.h > 0 );
  2604.  
  2605.     HSetState( (**hE).hText, hdlState );
  2606.  
  2607.     pt.h = left;
  2608.  
  2609.     /* may exceed limits of sixteen bits...
  2610.     pt.v = aPos.v * (**hE).vScale + (**hE).topMargin - (**hE).vOrigin;
  2611.     */
  2612.     pt.v = eTeChPosVToPointV( hE, aPos.v);
  2613.  
  2614.     return( pt );
  2615. }
  2616.  
  2617. static ChPos eTePointHtoChPosH( eRec **hE, register Point aPt, register ChPos aPos )
  2618. {
  2619.     register char *charPtr;
  2620.     register short left, c, limit;
  2621.     SignedByte hdlState;
  2622.     long     offset, instyle;
  2623.     short     run;
  2624.     Point    pt;
  2625.  
  2626.     hdlState = HGetState( (**hE).hText );
  2627.     HLock( (**hE).hText );
  2628.  
  2629.     eTePrepare( hE );
  2630.     /* Stop when we reach the point.h given to us */
  2631.     left = (**hE).leftMargin - (**hE).hOrigin;
  2632.     offset = *( *(**hE).hLines + aPos.v );
  2633.     charPtr = *(**hE).hText + offset;
  2634.     limit = *(*(**hE).hLines + aPos.v + 1) - *(*(**hE).hLines + aPos.v);
  2635.     /* 5Jul92  e  if ( aPos.v < (**hE).bounds.v - 1 ) --limit; */
  2636.     if ( charPtr[limit-1] == RETURN ) --limit;    /* 5Jul92  e   */
  2637.     run = eTeOffsetToRun( hE, offset );
  2638.     aPos.h = 0;
  2639.     do
  2640.     {    eTePrepareRun( hE, run );
  2641.         instyle = (*(**hE).hRuns)[++run] - offset;
  2642.         offset += instyle;
  2643.         while ( instyle-- && ( aPos.h < limit ) )
  2644.         {    c = *charPtr++;
  2645.             if ( c == TAB )
  2646.                 c = eTeTabStop( hE, left );
  2647.             else
  2648.                 c = CharWidth( c ) + left;
  2649.             /* see if we have passed the point */
  2650.             if ( c >= aPt.h )
  2651.             {    /* see if the point is in the second half of the character,
  2652.                    if so the returned value points to the next character. */
  2653.                 if ( c - aPt.h <= aPt.h - left ) aPos.h++;
  2654.                 limit = -1; /* force outer loop escape */
  2655.                 break;
  2656.             }
  2657.             left = c;
  2658.             aPos.h++;
  2659.         }
  2660.     } while( aPos.h < limit );
  2661.  
  2662.     HSetState( (**hE).hText, hdlState );
  2663.     return( aPos );
  2664. }
  2665.  
  2666. ChPos eTePointToChPos( eRec **hE, register Point aPt )
  2667. {
  2668.     register ChPos aPos;
  2669.  
  2670.     /* Restrict point to selectable text */
  2671.     if ( aPt.v < (**hE).topMargin - (**hE).vOrigin ) {
  2672.         aPos.v = 0;
  2673.         aPos.h = 0;
  2674.         return( aPos );
  2675.     }
  2676.     else {
  2677.         aPos.v = ( aPt.v - (**hE).topMargin + (**hE).vOrigin ) / (**hE).vScale;
  2678.         /* Past bottom of text? */
  2679.         if ( aPos.v > (**hE).bounds.v - 1 ) {
  2680.             aPos.v = (**hE).bounds.v - 1;
  2681.             aPos.h = *(*(**hE).hLines + aPos.v + 1) - *(*(**hE).hLines + aPos.v);
  2682.             return( aPos );
  2683.         }
  2684.     }
  2685.     return eTePointHtoChPosH( hE, aPt, aPos );
  2686. }
  2687.  
  2688. ChPos eTeOffsetToChPos( eRec **hE, register long anOffset )
  2689. {
  2690.     register ChPos aPos;
  2691.     register LineRec *linePtr;
  2692.     register long numLines, count, j;
  2693.  
  2694.     if ( anOffset < 0 ) {
  2695.         aPos.h = 0;
  2696.         aPos.v = 0;
  2697.         return( aPos );
  2698.     }
  2699.  
  2700.     linePtr = *(**hE).hLines;
  2701.     count = 0;
  2702.     numLines = (**hE).bounds.v - 1;
  2703. #ifdef use_original_slow_version
  2704.     while ( anOffset >= linePtr[ count + 1 ] && count < numLines )
  2705.         ++count;
  2706.     aPos.v = count;
  2707. #else
  2708.     while ( count < numLines )    {
  2709.         j = (count + numLines - 1) >> 1;
  2710.         if( anOffset < linePtr[ j ] )
  2711.             numLines = j;
  2712.         else if( anOffset >= linePtr[ j + 1 ] )
  2713.             count = j + 1;
  2714.         else    {
  2715.             count = j;
  2716.             break;
  2717.         }
  2718.     }
  2719.     aPos.v = count;
  2720. #endif
  2721.  
  2722.     if ( anOffset > linePtr[ count + 1 ] )
  2723.         aPos.h = linePtr[ count + 1 ] - linePtr[ count ];
  2724.     else
  2725.         aPos.h = anOffset - linePtr[ count ];
  2726.     return( aPos );
  2727. }
  2728.  
  2729. long eTeChPosToOffset( eRec **hE, ChPos aPos )
  2730. {
  2731.     return( *(*(**hE).hLines + aPos.v) + aPos.h );
  2732. }
  2733.  
  2734. void eTeShowCaret( eRec **hE )
  2735. {
  2736.     short        deltaH, deltaV;
  2737.     register eRec *pE = *hE;
  2738.  
  2739.     deltaH = 0;
  2740.     deltaV = 0;
  2741.  
  2742.     /* 30Apr92  e  -- was...
  2743.     if ( (*pE).caretRect.bottom > (*pE).viewRect.bottom )
  2744.         deltaV = ( (*pE).caretRect.bottom - (*pE).viewRect.bottom + (*pE).vScale - 1 ) / (*pE).vScale;
  2745.     else if ( (*pE).caretRect.top < (*pE).viewRect.top ) {
  2746.         deltaV = ( (*pE).caretRect.top - (*pE).viewRect.top ) / (*pE).vScale;
  2747.     */
  2748.     if ( (*pE).caretChPos.v >= (*pE).position.v + (*pE).vSpan )
  2749.         deltaV = (*pE).caretChPos.v - (*pE).position.v - (*pE).vSpan
  2750.                 + Min( (*pE).vContext, (*pE).vSpan >> 1 );
  2751.     else if ( (*pE).caretChPos.v < (*pE).position.v )
  2752.         deltaV = (*pE).caretChPos.v - (*pE).position.v
  2753.                  - Min( Min( (*pE).vContext, (*pE).vSpan >> 1 ), (*pE).caretChPos.v );
  2754.     /* NG for tabs and variable width fonts...
  2755.     if ( (*pE).caretChPos.h >= (*pE).position.h + (*pE).hSpan )
  2756.         deltaH = (*pE).caretChPos.h - (*pE).position.h - (*pE).hSpan + 1;
  2757.     else if ( (*pE).caretChPos.h < (*pE).position.h )
  2758.         deltaH = (*pE).caretChPos.h - (*pE).position.h;
  2759.     */
  2760.     if ( (*pE).caretRect.right > (*pE).viewRect.right )
  2761.         deltaH = ( (*pE).caretRect.right - (*pE).viewRect.right + (*pE).hScale - 1 ) / (*pE).hScale;
  2762.     else if ( (*pE).caretRect.left <= (*pE).viewRect.left ) /* was: (*pE).caretRect.right  22Jul92  e  */
  2763.     {    if( (*pE).caretRect.right + (*pE).hOrigin < (*pE).width )                        /* 22Jul92  e  */
  2764.             deltaH = - (*pE).hOrigin;                                                    /* 22Jul92  e  */
  2765.         else                                                                            /* 22Jul92  e  */
  2766.             deltaH = (*pE).caretRect.right;
  2767.         deltaH = ( deltaH - (*pE).viewRect.left - (*pE).hScale + 1 ) / (*pE).hScale;
  2768.     }
  2769.     if ( deltaH || deltaV )
  2770.     {    eTeScroll( hE, deltaH, deltaV, TRUE );
  2771.         eTeAdjustScrollMax( hE );
  2772.         eTeCalibrate( hE );
  2773.     }
  2774. }
  2775.  
  2776. /* scrolling (Panorama) */
  2777.  
  2778. /* 30Apr92  e  -- eTeScroll rewritten for >32K pixel document height */
  2779.  
  2780. void eTeScroll( eRec **hE, short hDelta, short vDelta, Boolean redraw )
  2781. {
  2782.     long            hPixels;
  2783.     long            vPixels;
  2784.     Rect            tempRect;
  2785.     register eRec    *pE;
  2786.  
  2787.     hPixels = (long )hDelta * (**hE).hScale;
  2788.     vPixels = (long )vDelta * (**hE).vScale;
  2789.  
  2790. #if 0                    /* 25Jan93  e  - to get rid of annoying inappropriate validation */
  2791.     if (redraw) {
  2792.         eTePrepare( hE );
  2793.         tempRect = (**hE).viewRect;
  2794.         if( vPixels < 32767 && vPixels > -32767 )
  2795.         {    ScrollRect( &tempRect, (short )-hPixels, (short )-vPixels, gUtilRgn );
  2796.             InvalRgn( gUtilRgn );
  2797.         }
  2798.         else InvalRect( &tempRect );
  2799.     }
  2800. #else
  2801.     if (redraw)
  2802.     {    eTePrepare( hE );
  2803.         tempRect = (**hE).viewRect;
  2804.         if( vPixels < (**hE).height && vPixels > -(**hE).height )
  2805.         {    if ( ! EmptyRgn( ((WindowPeek)(**hE).macPort)->updateRgn ) )
  2806.             {    BeginUpdate( (**hE).macPort );
  2807.                 eTeUpdate( hE );
  2808.                 EndUpdate( (**hE).macPort );
  2809.             }
  2810.             ScrollRect( &tempRect, (short )-hPixels, (short )-vPixels, gUtilRgn );
  2811.             InvalRgn( gUtilRgn );
  2812.         }
  2813.         else InvalRect( &tempRect );
  2814.     }
  2815. #endif
  2816.  
  2817.     pE = *hE;
  2818.     
  2819.     (*pE).position.h += hDelta;
  2820.     (*pE).position.v += vDelta;
  2821.  
  2822.     (*pE).hOrigin += hPixels;
  2823.     (*pE).vOrigin += vPixels;
  2824.     
  2825.     (*pE).maxRight    -= hPixels;
  2826.     (*pE).caretRect.left -= hPixels;
  2827.     (*pE).caretRect.right -= hPixels;
  2828.     (*pE).caretRect.top = eTeChPosVToPointV( hE, (*pE).caretChPos.v );
  2829.     (*pE).caretRect.bottom = (*pE).caretRect.top + (*pE).caretHeight;
  2830.  
  2831.     if (redraw) {
  2832.         BeginUpdate( (*pE).macPort );
  2833.         eTeUpdate( hE );
  2834.         EndUpdate( (**hE).macPort );
  2835.     }
  2836. }
  2837.  
  2838. void eTeScrollTo( eRec **hE, ChPos aPosition, Boolean redraw )
  2839. {
  2840.     eTeScroll( hE, aPosition.h - (**hE).position.h, aPosition.v - (**hE).position.v, redraw );
  2841.     eTeAdjustScrollMax( hE );
  2842.     eTeCalibrate( hE );
  2843. }
  2844.  
  2845. static Boolean eTeAutoScroll( eRec **hE, Point mouseLoc )
  2846. {
  2847.     short        hDelta = 0;
  2848.     short        vDelta = 0;
  2849.  
  2850.     if ( mouseLoc.h < (**hE).viewRect.left ) {
  2851.         hDelta = Max( -(**hE).hStep, -(**hE).position.h );
  2852.         if (hDelta > 0) {
  2853.             hDelta = 0;
  2854.         }
  2855.     } else if ( mouseLoc.h > (**hE).viewRect.right ) {
  2856.         hDelta = Min( (**hE).hStep, (**hE).bounds.h - (**hE).position.h - (**hE).hSpan );
  2857.         if (hDelta < 0) {
  2858.             hDelta = 0;
  2859.         }
  2860.     }
  2861.     
  2862.     if ( mouseLoc.v < (**hE).viewRect.top ) {
  2863.         vDelta = Max( -(**hE).vStep, -(**hE).position.v );
  2864.         if (vDelta > 0) {
  2865.             vDelta = 0;
  2866.         }
  2867.     } else if ( mouseLoc.v >=
  2868.                     /* (**hE).viewRect.bottom */
  2869.                     (**hE).viewRect.top + (**hE).vSpan * (**hE).vScale
  2870.                      ) {
  2871.         vDelta = Min( (**hE).vStep, (**hE).bounds.v - (**hE).position.v - (**hE).vSpan );
  2872.         if (vDelta < 0) {
  2873.             vDelta = 0;
  2874.         }
  2875.     }
  2876.     
  2877.     if ( (hDelta != 0) || (vDelta != 0) ) {
  2878.         eTeScroll( hE, hDelta, vDelta, TRUE );
  2879.         eTeCalibrate( hE );
  2880.         eTePrepare( hE );
  2881.         return(TRUE);
  2882.     } else {
  2883.         return(FALSE);
  2884.     }
  2885. }
  2886.  
  2887. static void eSbPrepare( eRec **hE )
  2888. {
  2889.     SetPort( (**hE).macPort );
  2890.     ClipRect( &(**hE).macPort->portRect );
  2891. }
  2892.  
  2893. static void eTeAdjustScrollMax( eRec **hE )
  2894. {
  2895.     short            hSpan;
  2896.     short            vSpan;
  2897.     
  2898.     eSbPrepare( hE );
  2899.  
  2900.     (**hE).hSpan = hSpan = (**hE).width / (**hE).hScale;
  2901.     (**hE).vSpan = vSpan = (**hE).height / (**hE).vScale;
  2902.     
  2903.     if ( (**hE).hSBar != NULL ) {
  2904.         SetCtlMax( (**hE).hSBar, Max( ( (**hE).bounds.h - hSpan ), (**hE).position.h ) );
  2905.     }
  2906.  
  2907.     if ( (**hE).vSBar != NULL ) {
  2908.         SetCtlMax( (**hE).vSBar, Max( ( (**hE).bounds.v - vSpan ), (**hE).position.v ) );
  2909.     }
  2910. }
  2911.  
  2912. static void eTeCalibrate( eRec **hE )
  2913. {
  2914.     eSbPrepare( hE );
  2915.  
  2916.     if ( (**hE).hSBar != NULL ) {
  2917.         SetCtlValue( (**hE).hSBar, (**hE).position.h );
  2918.     }
  2919.     if ( (**hE).vSBar != NULL ) {
  2920.         SetCtlValue( (**hE).vSBar, (**hE).position.v );
  2921.     }
  2922. }
  2923.  
  2924. static short countKeys()
  2925. {    KeyMap km;
  2926.     register short count = 1;
  2927.     register short i = 4;
  2928.     register long kmi;
  2929.     
  2930.     GetKeys( km );
  2931.     while( i-- )
  2932.     {    kmi = km[i];
  2933.         while( kmi )
  2934.         {    count <<= 1;
  2935.             kmi &= (kmi-1);
  2936.         }
  2937.     }
  2938.     return count;
  2939. }
  2940.  
  2941. static void eTeDoHscroll( eRec **hE, short whichPart )
  2942. {
  2943.     register short        delta;            /* Number of pixels to scroll        */
  2944.     short                oldValue;        /* Current scroll bar setting        */
  2945.     register short        minmax;            /* Minimum or Maximum delta            */
  2946.     long                ticks;            /* Tick count at end of Delay        */
  2947.  
  2948.     switch (whichPart) {
  2949.         case inUpButton:
  2950.             delta = -( (**hE).hStep * countKeys() );
  2951.             break;
  2952.         case inDownButton:
  2953.             delta =  ( (**hE).hStep * countKeys() );
  2954.             break;
  2955.         case inPageUp:
  2956.             Delay(PAGE_DELAY, &ticks);
  2957.             delta = (**hE).hOverlap - (**hE).hSpan;
  2958.             break;
  2959.         case inPageDown:
  2960.             Delay(PAGE_DELAY, &ticks);
  2961.             delta = (**hE).hSpan - (**hE).hOverlap;
  2962.             break;
  2963.     }
  2964.     oldValue = GetCtlValue( (**hE).hSBar );
  2965.     if (delta < 0) {
  2966.         minmax = GetCtlMin( (**hE).hSBar ) - oldValue;
  2967.         if (delta < minmax)
  2968.             delta = minmax;
  2969.     } else {
  2970.         minmax = GetCtlMax( (**hE).hSBar ) - oldValue;
  2971.         if (delta > minmax)
  2972.             delta = minmax;
  2973.     }
  2974.     if (delta != 0) {
  2975.         eSbPrepare( hE );
  2976.         SetCtlValue( (**hE).hSBar, oldValue + delta );
  2977.         eTeScroll( hE, delta, 0, TRUE );
  2978.         eSbPrepare( hE );
  2979.     }
  2980. }
  2981.     
  2982. static void eTeDoVscroll( eRec **hE, short whichPart )
  2983. {
  2984.     register short        delta;            /* Number of pixels to scroll        */
  2985.     short                oldValue;        /* Current scroll bar setting        */
  2986.     register short        minmax;            /* Minimum or Maximum delta            */
  2987.     long                ticks;            /* Tick count at end of Delay        */
  2988.  
  2989.     switch (whichPart) {
  2990.         case inUpButton:
  2991.             delta = -( (**hE).vStep * countKeys() );
  2992.             break;
  2993.         case inDownButton:
  2994.             delta =  ( (**hE).vStep * countKeys() );
  2995.             break;
  2996.         case inPageUp:
  2997.             Delay(PAGE_DELAY, &ticks);
  2998.             delta = (**hE).vOverlap - (**hE).vSpan;
  2999.             break;
  3000.         case inPageDown:
  3001.             Delay(PAGE_DELAY, &ticks);
  3002.             delta = (**hE).vSpan - (**hE).vOverlap;
  3003.             break;
  3004.     }
  3005.     oldValue = GetCtlValue( (**hE).vSBar );
  3006.     if (delta < 0) {
  3007.         minmax = GetCtlMin( (**hE).vSBar ) - oldValue;
  3008.         if (delta < minmax)
  3009.             delta = minmax;
  3010.     } else {
  3011.         minmax = GetCtlMax( (**hE).vSBar ) - oldValue;
  3012.         if (delta > minmax)
  3013.             delta = minmax;
  3014.     }
  3015.     if (delta != 0) {
  3016.         eSbPrepare( hE );
  3017.         SetCtlValue( (**hE).vSBar, oldValue + delta );
  3018.         eTeScroll( hE, 0, delta, TRUE );
  3019.         eSbPrepare( hE );
  3020.     }
  3021. }
  3022.     
  3023. /* called continuously while mouse down within a fixed part of a scroll bar.
  3024.    Called by the Toolbox during the TrackControl trap. */
  3025.  
  3026. static pascal void hSBarActionProc( ControlHandle macControl, short whichPart )
  3027. {
  3028.     eRec    **hE; 
  3029.     
  3030.     if (whichPart != 0) {
  3031.         hE = (eRec **)GetCRefCon( macControl );
  3032.         eTeDoHscroll( hE, whichPart );
  3033.     }
  3034. }
  3035.  
  3036. static pascal void vSBarActionProc( ControlHandle macControl, short whichPart )
  3037. {
  3038.     eRec    **hE; 
  3039.     
  3040.     if (whichPart != 0) {
  3041.         hE = (eRec **) GetCRefCon( macControl );
  3042.         eTeDoVscroll( hE, whichPart );
  3043.     }
  3044. }
  3045.  
  3046. /* end of os_mac_eEdit.c */